跳至内容
Odoo 菜单
  • 登录
  • 免费试用
  • 应用程序
    财务
    • 会计
    • 发票
    • 费用
    • 电子表格 (BI)
    • 文档
    • 电子签名
    销售
    • 客户关系管理
    • 销售
    • POS 销售点管理-零售
    • POS 销售点管理 - 餐厅
    • 订阅
    • 租赁
    网站
    • 网站设计
    • 电子商务
    • 博客
    • 论坛
    • 在线客服
    • 在线学习
    供应链
    • 库存
    • 制造
    • 产品生命周期
    • 采购
    • 维护保养
    • 品控
    人力资源
    • 员工
    • 招聘
    • 休假
    • 评价
    • 内部推荐
    • 车队
    营销
    • 社媒营销
    • 电邮营销
    • 短信营销
    • 近期活动
    • 营销自动化
    • 网上调查
    服务
    • 项目管理
    • 工时单
    • 现场服务
    • 服务台
    • 排期
    • 预约
    生产力
    • 讨论
    • 批核
    • IoT物联网
    • VoIP
    • 知识库
    • WhatsApp
    第三方应用软件 Odoo 定制 Odoo云端平台
  • 行业
    零售
    • 书店
    • 服装店
    • 家具店
    • 食品杂货店
    • 五金店
    • 玩具店
    餐饮与酒店服务
    • 酒吧及酒馆
    • 餐厅
    • 快餐
    • 民宿
    • 饮品分销商
    • 酒店
    房地产
    • 房地产代理
    • 建筑师事务所
    • 建造业
    • 地产管理
    • 园艺
    • 业主协会
    咨询
    • 会计师事务所
    • Odoo合作伙伴
    • 市场推广公司
    • 律师事务所
    • 人才招聘
    • 审核 & 认证
    制造
    • 纺织
    • 金属
    • 家具
    • 食品
    • 啤酒厂
    • 企业礼品
    保健与健身
    • 体育俱乐部
    • 眼镜店
    • 健身中心
    • 健康从业者
    • 药房
    • 发型屋
    商贸服务
    • 维修人员
    • IT 硬件及支持
    • 太阳能系统
    • 鞋匠
    • 清洁服务
    • 暖通空调服务
    其他
    • 非营利组织
    • 环境机构
    • 广告牌租赁
    • 摄影服务
    • 自行车租赁
    • 软件经销商
    浏览所有行业
  • 社区
    学习
    • 教学视频
    • 文档
    • 认证
    • 培训
    • 博客
    • 播客
    赋能教育
    • 教育计划
    • Scale Up! 商业游戏
    • 参观Odoo
    获取软件
    • 下载
    • 版本对比
    • 发布
    合作
    • Github
    • 论坛
    • 近期活动
    • 翻译
    • 成为合作伙伴
    • 合作伙伴服务
    • 注册您的会计事务所
    获取服务
    • 寻找合作伙伴
    • 查找会计服务
    • 预约顾问咨询
    • 安装及推行服务
    • 客户参考
    • 支持
    • 升级
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    获取演示
  • 定价
  • 技术支持

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • 客户关系管理
  • e-Commerce
  • 会计
  • 库存
  • PoS
  • 项目
  • MRP
All apps
只限注册用戶才可与社群互动。
所有帖文 人 徽章
标签 (查看所有)
odoo accounting v14 pos v15
关于此论坛区
只限注册用戶才可与社群互动。
所有帖文 人 徽章
标签 (查看所有)
odoo accounting v14 pos v15
关于此论坛区
帮助

How to save and update custom settings in Odoo? (TransientModel)

订阅

此帖文有活动时,接收通知

此问题已终结
configurationsettingstransientmodelodoo8.0
6 回复
16153 查看
形象
Yenthe Van Ginneken (Mainframe Monkey)

Hi guys,

I've just created a new custom settings menuitem under Settings (just like other ones exist for Sales, Financial, ..) I create a new TransientModel which holds the temp data like this:

class ResConfigOutlook(models.TransientModel):
    _name = 'outlook.sync.settings'
    _inherit = 'res.config.settings'

outlook_id = fields.Many2one('outlook.sync', string='Outlook', required=True, defaults=lambda self,cr,uid,c: self.pool.get('outlook.sync').search(cr, uid, [], context=c)[0]) sync_calendars = fields.Boolean(related='outlook_id.sync_calendars', default=False) sync_contacts = fields.Boolean(related='outlook_id.sync_contacts', default=False) default_backwards_sync = fields.Integer(related='outlook_id.default_backwards_sync', default='1') default_forward_sync = fields.Integer(related='outlook_id.default_forward_sync', default='3') def on_change_outlook_id(self, cr, uid, ids, outlook_id, context=None): if not outlook_id: #This means it was never configged - default values here! return {'value': {'sync_calendars': False, 'sync_contacts': False, 'default_backwards_sync': 1, 'default_forward_sync': 3}} outlook_data = self.pool.get('outlook.sync').read(cr, uid, [outlook_id], [], context=context)[0] values = { 'sync_calendars': outlook_data['sync_calendars'], 'sync_contacts': outlook_data['sync_contacts'], 'default_backwards_sync': outlook_data['default_backwards_sync'], 'default_forward_sync': outlook_data['default_forward_sync']} for fname, v in outlook_data.items(): if fname in self._columns: values[fname] = v[0] if v and self._columns[fname]._type == 'many2one' else v return {'value': values}

def create(self, cr, uid, vals, context=None): config_id = super(ResConfigOutlook, self).create(cr, uid, vals, context=context) self.write(cr, uid, config_id, vals, context=context) return config_id

The outlook_id (many2one) links to the model that will actually hold the data, outlook.sync which contains the following fields:

class Settings_holder(models.Model):
    _name = 'outlook.sync'
    sync_calendars = fields.Boolean('Info')
    sync_contacts = fields.Boolean('Info')
    default_backwards_sync = fields.Integer('Info')
    default_forward_sync = fields.Integer('Info')

I then created a new view to show all these values:

<record id="outlook_sync_config" model="ir.ui.view">
<field name="name">Your configuration</field>
<field name="model">outlook.sync.settings</field>
<field name="arch" type="xml">
    <form string="Outlook configuration" class="oe_form_configuration">
         <header>
             <button string="Save" type="object"name="execute" class="oe_highlight"/>
             or 
             <button string="Cancel" type="object" name="cancel" class="oe_link"/>
      </header>
<field name="outlook_id" invisible="True" on_change="on_change_outlook_id(outlook_id)"/>; <group string="Synchronise"> <field name="sync_calendars" string="Synchronise calendars"/> <field name="sync_contacts" string="Synchronise contacts"/> </group> <group string="Outlook settings"> <field name="default_backwards_sync" string="Backwards sync"/> <field name="default_forward_sync" string="Forward sync"/>
</group> </form> </field> </record>

Now this all looks good, until I hit the 'Save' button. I will then get the following error:

Integrity Error

The operation cannot be completed, probably due to the following: - deletion: you may be trying to delete a record while other records still reference it - creation/update: a mandatory field is not correctly set
[object with reference: outlook_id - outlook.id]

But now I'm wondering.. how do I transfer the data from the TransientModel to the normal model and the other way around? I seem to be doing something wrong.

Yenthe

2
形象
丢弃
形象
Axel Mendoza
最佳答案

Hi @Yenthe

Take as example website settings and their use of related on settings fields

------------------------------------------------------------------------------- Changes---------------------------------------------------------------------------------

# -*- coding: utf-8 -*-
from openerp import models, fields, api
from openerp.osv import osv

#Import logger
import logging
#Get the logger
_logger = logging.getLogger(__name__)

class Settings_holder(models.Model):
_name = 'outlook.sync'
sync_calendars = fields.Boolean('Sync Odoo and Outlook calendars', default=False)
sync_contacts = fields.Boolean('Sync Odoo and outlook contacts')
default_backwards_sync = fields.Integer('Months of backwards syncing')
default_forward_sync = fields.Integer('Months of forward syncing')


class ResConfigOutlook(models.TransientModel):
_name = 'outlook.sync.settings'
_inherit = 'res.config.settings'

outlook_id = fields.Many2one('outlook.sync', string='Outlook')
sync_calendars = fields.Boolean(related='outlook_id.sync_calendars')
sync_contacts = fields.Boolean(related='outlook_id.sync_contacts')
default_backwards_sync = fields.Integer(related='outlook_id.default_backwards_sync')
default_forward_sync = fields.Integer(related='outlook_id.default_forward_sync')

def on_change_outlook_id(self, cr, uid, ids, outlook_id, context=None):
_logger.critical('ON_CHANGE_OUTLOOK_ID')
if not outlook_id:
#This means it was never configged - default values here!
return {'value': {'sync_calendars': False, 'sync_contacts': False, 'default_backwards_sync': 1, 'default_forward_sync': 3}}
outlook_data = self.pool.get('outlook.sync').read(cr, uid, [outlook_id], [], context=context)[0]
values = {
'sync_calendars': outlook_data['sync_calendars'],
'sync_contacts': outlook_data['sync_contacts'],
'default_backwards_sync': outlook_data['default_backwards_sync'],
'default_forward_sync': outlook_data['default_forward_sync']
}
for fname, v in outlook_data.items():
if fname in self._columns:
values[fname] = v[0] if v and self._columns[fname]._type == 'many2one' else v
return {'value': values}

def create(self, cr, uid, vals, context=None):
_logger.critical('CREATE' + str (vals))
config_id = super(ResConfigOutlook, self).create(cr, uid, vals, context=context)
self.write(cr, uid, config_id, vals, context=context)
return config_id

_defaults = {
'outlook_id': lambda self,cr,uid,c: self.pool.get('outlook.sync').search(cr, uid, [], context=c)[0]
}

I create a view, action and menu for test it

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>

<record id="view_website_config_settings" model="ir.ui.view">
<field name="name">Website settings</field>
<field name="model">outlook.sync.settings</field>
<field name="arch" type="xml">
<form string="Configure Outlook" class="oe_form_configuration">
<header>
<button string="Apply" type="object" name="execute" class="oe_highlight"/>
or
<button string="Cancel" type="object" name="cancel" class="oe_link"/>
</header>
<div>
<field name="outlook_id" invisible="True" on_change="on_change_outlook_id(outlook_id)"/>
<group string="Synchronise">
<field name="sync_calendars" />
<field name="sync_contacts"/>
</group>
<group string="Outlook settings">
<field name="default_backwards_sync"/>
<field name="default_forward_sync"/>
</group>
</div>
</form>
</field>
</record>

<record id="action_outlook_configuration" model="ir.actions.act_window">
<field name="name">Outlook Settings</field>
<field name="res_model">outlook.sync.settings</field>
<field name="view_mode">form</field>
<field name="target">inline</field>
</record>

<menuitem id="menu_outlook_configuration" parent="base.menu_config"
sequence="90" action="action_outlook_configuration"/>

</data>
<data noupdate="1">
<record id="default_outlook" model="outlook.sync">
<field name="sync_calendars" eval="True"/>
<field name="sync_contacts" eval="True"/>
<field name="default_backwards_sync" type="int">1</field>
<field name="default_forward_sync" type="int">1</field>
</record>
</data>

</openerp>

As you see on the end of the xml there is the definition/creation of the record to be updated in the settings view, the one obtained from the _defaults for outlook_id and passed to the onchange. I test it and it's working


5
形象
丢弃
Yenthe Van Ginneken (Mainframe Monkey)
编写者

@Axel I've updated my question with the newest code - I'm almost there I think. Now I get an integrity Error.. Any idea?

Yenthe Van Ginneken (Mainframe Monkey)
编写者

@Axel update #2: no more errors but the values do not seem to be saved / migrated from one table to another and are not shown to the user.

Bole

Hey.. i don't see any relation to user from outlook.setting model... Maybe if that is supposed to be for users, you would like to make class outlook_settings(models.Model): _inherit = 'res.users' ??? that way it will extend user class and should be easily wievable to users...

Axel Mendoza

@Yenthe I test it your code and I will update my answer with the changes that works

Yenthe Van Ginneken (Mainframe Monkey)
编写者

@Bole: there is no relation to an user, these are system wide settings. So this only has to be done once for the whole company! :) @Axel: thanks a lot for the great answer! This is a nice approach to get things working and I really like it. +1 and accepted. Thanks a lot for your time :)

Axel Mendoza

Thanks a lot for you guys, all of this problems and solutions will help for those who will look for similar problems in the future

Yenthe Van Ginneken (Mainframe Monkey)
编写者

Exactly! One thing I'm wondering: couldn't we rewrite the defaults to V8/V9 API style?

Axel Mendoza

In the new api it need to be like this:
outlook_id = fields.Many2one('outlook.sync', string='Outlook', required=True, default=lambda self: self.env['outlook.sync'].search([])[0])

jianxin

Hi @Yenthe, thanks for your solution, I almost done with my custom setting function.But I have got one problem -- the cancel button also save the settings, seems that the TransientModel auto save data when any button clicked. One more question, for your code above, since there is a default function for outlook_id, what's the purpose of the on_change function.

形象
Emipro Technologies Pvt. Ltd.
最佳答案

Hello Yenthe,

You have done correct implementation. But you just missed on method which is very important and define inside "res.config.setting" model and called when user click on "Apply" button. That is looks like below.

 @api.multi
def execute(self):
res = super(ResConfigOutlook,self).execute()
...... #Your code
return res

As your code you have to just write down one method as like below and your task is over. 

 @api.multi
def execute(self):
outlook_object = self.outlook_id
values = {}
res = super(ResConfigOutlook,self).execute()
if outlook_object:
values['sync_calendars'] = self.sync_calendars or False
values['sync_contacts'] = self.sync_contacts or False
values['default_backwards_sync'] = self.default_backwards_sync or False
values['default_forward_sync'] = self.default_forward_sync or False
outlook_object.write(values)

return res

"""how do I transfer the data from the TransientModel to the normal model ? """. There is one method named "execute" is responsible for transfer the data which is define inside res.config.settings model. I am sure that you get your answer and fix your issue.

Thanks.


1
形象
丢弃
Yenthe Van Ginneken (Mainframe Monkey)
编写者

Thanks a lot for your answer and time Emipro! Your solution works with some minor changes but I do like the method from Axel a bit more. I've upvoted your answer too though. Sorry that I can't accept both!

Emipro Technologies Pvt. Ltd.

It's fine. As Axel's answer is better then me. I will upvote his answer too.

喜欢讨论吗?不要只阅读,加入进来!

立即创建账户,享受专属功能,与我们的精彩社区互动!

注册
相关帖文 回复 查看 活动
Add custom field in Settings->Configuration->Sales 已解决
configuration v8 settings transientmodel
形象
形象
形象
2
10月 16
12466
disable delete and edit options in conversations Odoo 15
configuration settings
形象
形象
形象
2
4月 24
5413
How to define custom settings on configuration view? 已解决
configuration odoo8.0
形象
形象
形象
8
2月 24
13519
What are the things to watch out for before my OpenERP goes live? Version 7.
configuration settings
形象
0
3月 15
8183
Reserve Profit and Loss Account
configuration settings
形象
形象
1
3月 15
7529
社区
  • 教学视频
  • 文档
  • 论坛
开源
  • 下载
  • Github
  • Runbot
  • 翻译
服务
  • Odoo.sh 托管
  • 支持
  • 升级
  • 自定义开发服务
  • 教育
  • 查找会计服务
  • 寻找合作伙伴
  • 成为合作伙伴
关于我们
  • 我们的公司
  • 品牌资产
  • 联系我们
  • 招聘
  • 近期活动
  • 播客
  • 博客
  • 客户
  • 法律 • 隐私
  • 安全
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo致力于为企业管理提供高效智能的开源解决方案,是全球业内高速成长的软件服务商之一,逾七百五十万用户选择Odoo进行数字化升级。通过一系列全业务链覆盖、高度集成、简单易用的商业应用,助力企业实现信息化改革、降本增效并释放公司增长潜力。

Odoo独特的价值在于是一款非常容易使用又完全集成的应用。

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now