Passa al contenuto
Odoo Menu
  • Accedi
  • Provalo gratis
  • App
    Finanze
    • Contabilità
    • Fatturazione
    • Note spese
    • Fogli di calcolo (BI)
    • Documenti
    • Firma
    Vendite
    • CRM
    • Vendite
    • Punto vendita Negozio
    • Punto vendita Ristorante
    • Abbonamenti
    • Noleggi
    Siti web
    • Configuratore sito web
    • E-commerce
    • Blog
    • Forum
    • Live chat
    • E-learning
    Supply chain
    • Magazzino
    • Produzione
    • PLM
    • Acquisti
    • Manutenzione
    • Qualità
    Risorse umane
    • Dipendenti
    • Assunzioni
    • Ferie
    • Valutazioni
    • Referral dipendenti
    • Parco veicoli
    Marketing
    • Social marketing
    • E-mail marketing
    • SMS marketing
    • Eventi
    • Marketing automation
    • Sondaggi
    Servizi
    • Progetti
    • Fogli ore
    • Assistenza sul campo
    • Helpdesk
    • Pianificazione
    • Appuntamenti
    Produttività
    • Comunicazioni
    • Approvazioni
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    App di terze parti Odoo Studio Piattaforma cloud Odoo
  • Settori
    Retail
    • Libreria
    • Negozio di abbigliamento
    • Negozio di arredamento
    • Alimentari
    • Ferramenta
    • Negozio di giocattoli
    Cibo e ospitalità
    • Bar e pub
    • Ristorante
    • Fast food
    • Pensione
    • Grossista di bevande
    • Hotel
    Agenzia immobiliare
    • Agenzia immobiliare
    • Studio di architettura
    • Edilizia
    • Gestione immobiliare
    • Impresa di giardinaggio
    • Associazione di proprietari immobiliari
    Consulenza
    • Società di contabilità
    • Partner Odoo
    • Agenzia di marketing
    • Studio legale
    • Selezione del personale
    • Audit e certificazione
    Produzione
    • Tessile
    • Metallo
    • Arredamenti
    • Alimentare
    • Birrificio
    • Ditta di regalistica aziendale
    Benessere e sport
    • Club sportivo
    • Negozio di ottica
    • Centro fitness
    • Centro benessere
    • Farmacia
    • Parrucchiere
    Commercio
    • Tuttofare
    • Hardware e assistenza IT
    • Ditta di installazione di pannelli solari
    • Calzolaio
    • Servizi di pulizia
    • Servizi di climatizzazione
    Altro
    • Organizzazione non profit
    • Ente per la tutela ambientale
    • Agenzia di cartellonistica pubblicitaria
    • Studio fotografico
    • Punto noleggio di biciclette
    • Rivenditore di software
    Carica tutti i settori
  • Community
    Apprendimento
    • Tutorial
    • Documentazione
    • Certificazioni 
    • Formazione
    • Blog
    • Podcast
    Potenzia la tua formazione
    • Programma educativo
    • Scale Up! Business Game
    • Visita Odoo
    Ottieni il software
    • Scarica
    • Versioni a confronto
    • Note di versione
    Collabora
    • Github
    • Forum
    • Eventi
    • Traduzioni
    • Diventa nostro partner
    • Servizi per partner
    • Registra la tua società di contabilità
    Ottieni servizi
    • Trova un partner
    • Trova un contabile
    • Incontra un esperto
    • Servizi di implementazione
    • Testimonianze dei clienti
    • Supporto
    • Aggiornamenti
    GitHub Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Richiedi una demo
  • Prezzi
  • Aiuto

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

  • CRM
  • e-Commerce
  • Contabilità
  • Magazzino
  • PoS
  • Progetti
  • MRP
All apps
È necessario essere registrati per interagire con la community.
Tutti gli articoli Persone Badge
Etichette (Mostra tutto)
odoo accounting v14 pos v15
Sul forum
È necessario essere registrati per interagire con la community.
Tutti gli articoli Persone Badge
Etichette (Mostra tutto)
odoo accounting v14 pos v15
Sul forum
Assistenza

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

Iscriviti

Ricevi una notifica quando c'è un'attività per questo post

La domanda è stata contrassegnata
configurationsettingstransientmodelodoo8.0
6 Risposte
16159 Visualizzazioni
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
Abbandona
Avatar
Axel Mendoza
Risposta migliore

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
Abbandona
Yenthe Van Ginneken (Mainframe Monkey)
Autore

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

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

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

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.
Risposta migliore

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
Abbandona
Yenthe Van Ginneken (Mainframe Monkey)
Autore

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.

Ti stai godendo la conversazione? Non leggere soltanto, partecipa anche tu!

Crea un account oggi per scoprire funzionalità esclusive ed entrare a far parte della nostra fantastica community!

Registrati
Post correlati Risposte Visualizzazioni Attività
Add custom field in Settings->Configuration->Sales Risolto
configuration v8 settings transientmodel
Avatar
Avatar
Avatar
2
ott 16
12469
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? Risolto
configuration odoo8.0
Avatar
Avatar
Avatar
8
feb 24
13521
What are the things to watch out for before my OpenERP goes live? Version 7.
configuration settings
Avatar
0
mar 15
8184
Reserve Profit and Loss Account
configuration settings
Avatar
Avatar
1
mar 15
7530
Community
  • Tutorial
  • Documentazione
  • Forum
Open source
  • Scarica
  • Github
  • Runbot
  • Traduzioni
Servizi
  • Hosting Odoo.sh
  • Supporto
  • Aggiornamenti
  • Sviluppi personalizzati
  • Formazione
  • Trova un contabile
  • Trova un partner
  • Diventa nostro partner
Chi siamo
  • La nostra azienda
  • Branding
  • Contattaci
  • Lavora con noi
  • Eventi
  • Podcast
  • Blog
  • Clienti
  • Note legali • Privacy
  • Sicurezza
الْعَرَبيّة 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 è un gestionale di applicazioni aziendali open source pensato per coprire tutte le esigenze della tua azienda: CRM, Vendite, E-commerce, Magazzino, Produzione, Fatturazione elettronica, Project Management e molto altro.

Il punto di forza di Odoo è quello di offrire un ecosistema unico di app facili da usare, intuitive e completamente integrate tra loro.

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