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