Skip ke Konten
Odoo Menu
  • Login
  • Uji coba gratis
  • Aplikasi
    Keuangan
    • Akuntansi
    • Faktur
    • Pengeluaran
    • Spreadsheet (BI)
    • Dokumen
    • Tanda Tangan
    Sales
    • CRM
    • Sales
    • POS Toko
    • POS Restoran
    • Langganan
    • Rental
    Website
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Rantai Pasokan
    • Inventaris
    • Manufaktur
    • PLM
    • Purchase
    • Maintenance
    • Kualitas
    Sumber Daya Manusia
    • Karyawan
    • Rekrutmen
    • Cuti
    • Appraisal
    • Referensi
    • Armada
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Acara
    • Otomatisasi Marketing
    • Survei
    Layanan
    • Project
    • Timesheet
    • Layanan Lapangan
    • Meja Bantuan
    • Planning
    • Appointment
    Produktivitas
    • Diskusi
    • Approval
    • IoT
    • VoIP
    • Pengetahuan
    • WhatsApp
    Aplikasi pihak ketiga Odoo Studio Platform Odoo Cloud
  • Industri-Industri
    Retail
    • Toko Buku
    • Toko Baju
    • Toko Furnitur
    • Toko Kelontong
    • Toko Hardware
    • Toko Mainan
    Makanan & Hospitality
    • Bar dan Pub
    • Restoran
    • Fast Food
    • Rumah Tamu
    • Distributor Minuman
    • Hotel
    Real Estate
    • Agensi Real Estate
    • Firma Arsitektur
    • Konstruksi
    • Estate Management
    • Perkebunan
    • Asosiasi Pemilik Properti
    Konsultansi
    • Firma Akuntansi
    • Mitra Odoo
    • Agensi Marketing
    • Firma huku
    • Talent Acquisition
    • Audit & Sertifikasi
    Manufaktur
    • Tekstil
    • Logam
    • Perabotan
    • Makanan
    • Brewery
    • Corporate Gift
    Kesehatan & Fitness
    • Sports Club
    • Toko Kacamata
    • Fitness Center
    • Wellness Practitioners
    • Farmasi
    • Salon Rambut
    Perdagangan
    • Handyman
    • IT Hardware & Support
    • Sistem-Sistem Energi Surya
    • Pembuat Sepatu
    • Cleaning Service
    • Layanan HVAC
    Lainnya
    • Organisasi Nirlaba
    • Agen Lingkungan
    • Rental Billboard
    • Fotografi
    • Penyewaan Sepeda
    • Reseller Software
    Browse semua Industri
  • Komunitas
    Belajar
    • Tutorial-tutorial
    • Dokumentasi
    • Sertifikasi
    • Pelatihan
    • Blog
    • Podcast
    Empower Education
    • Program Edukasi
    • Game Bisnis 'Scale Up!'
    • Kunjungi Odoo
    Dapatkan Softwarenya
    • Download
    • Bandingkan Edisi
    • Daftar Rilis
    Kolaborasi
    • Github
    • Forum
    • Acara
    • Terjemahan
    • Menjadi Partner
    • Layanan untuk Partner
    • Daftarkan perusahaan Akuntansi Anda.
    Dapatkan Layanan
    • Temukan Mitra
    • Temukan Akuntan
    • Temui penasihat
    • Layanan Implementasi
    • Referensi Pelanggan
    • Bantuan
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dapatkan demo
  • Harga
  • Bantuan

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

  • CRM
  • e-Commerce
  • Akuntansi
  • Inventaris
  • PoS
  • Project
  • MRP
All apps
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Help

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

Langganan

Dapatkan notifikasi saat terdapat aktivitas pada post ini

Pertanyaan ini telah diberikan tanda
configurationsettingstransientmodelodoo8.0
6 Replies
16152 Tampilan
Avatar
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
Avatar
Buang
Avatar
Axel Mendoza
Jawaban Terbai

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
Avatar
Buang
Yenthe Van Ginneken (Mainframe Monkey)
Penulis

@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)
Penulis

@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)
Penulis

@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)
Penulis

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.

Avatar
Emipro Technologies Pvt. Ltd.
Jawaban Terbai

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
Avatar
Buang
Yenthe Van Ginneken (Mainframe Monkey)
Penulis

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.

Menikmati diskusi? Jangan hanya membaca, ikuti!

Buat akun sekarang untuk menikmati fitur eksklufi dan agar terlibat dengan komunitas kami!

Daftar
Post Terkait Replies Tampilan Aktivitas
Add custom field in Settings->Configuration->Sales Diselesaikan
configuration v8 settings transientmodel
Avatar
Avatar
Avatar
2
Okt 16
12465
disable delete and edit options in conversations Odoo 15
configuration settings
Avatar
Avatar
Avatar
2
Apr 24
5413
How to define custom settings on configuration view? Diselesaikan
configuration odoo8.0
Avatar
Avatar
Avatar
8
Feb 24
13519
What are the things to watch out for before my OpenERP goes live? Version 7.
configuration settings
Avatar
0
Mar 15
8183
Reserve Profit and Loss Account
configuration settings
Avatar
Avatar
1
Mar 15
7529
Komunitas
  • Tutorial-tutorial
  • Dokumentasi
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Terjemahan
Layanan
  • Odoo.sh Hosting
  • Bantuan
  • Peningkatan
  • Custom Development
  • Pendidikan
  • Temukan Akuntan
  • Temukan Mitra
  • Menjadi Partner
Tentang Kami
  • Perusahaan kami
  • Aset Merek
  • Hubungi kami
  • Tugas
  • Acara
  • Podcast
  • Blog
  • Pelanggan
  • Hukum • Privasi
  • Keamanan
الْعَرَبيّة 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 adalah rangkaian aplikasi bisnis open source yang mencakup semua kebutuhan perusahaan Anda: CRM, eCommerce, akuntansi, inventaris, point of sale, manajemen project, dan seterusnya.

Mudah digunakan dan terintegrasi penuh pada saat yang sama adalah value proposition unik 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