Skip to Content
Menu
This question has been flagged

Hi,

My requirement as brief:

  • Root contact is a contact whose parent_id is None
  • I don't want to have *root contacts* with the same email/country
  • I need to use sql_constraints to fix this issue
  • I need to define a new field named root_contact for all contacts and set it to 0 or 1 for all contacts

So,

I've defined root_contact as:

root_contact = fields.Integer(
​string="Root Contact",
​compute="_compute_root_contact",
​copy=False,
​store=True
)
And the function is:
def_compute_root_contact(self):
​if(self.parent_id):
​self.root_contact = 0
​else:
​self.root_contact = 1

As there are many previous contacts in the contact list, how can I set this field for all contacts?

Later, I want to set sql_constraints as below:

_sql_constraints = [        
​('email_country_parent_uniq', 'UNIQUE(root_contact,email,country_id)',
​'A root contact with the same info is presented before. \
​ Please edit that contact or define a new one with different info.')
]

Problems:

  1. If I use store=True the compute function doesn't call at all, and in this case, based on the Odoo document, I need to set @api.onchange to be able to call this method (and this is not the thing I want).
  2. If I don't use store=True so the field doesn't store in the database and so, I could not use the field in sql_constraints

From Postgres 15, a new feature is added named 'NULLS NOT DISTINCT' that can be used with the combination of UNIQUE in sql_constraints and this is the best-fit choice for my requirement but I'm using Postgres 14.

Avatar
Discard
Best Answer

Hi,

Try use @api.depends with a computed field to store in the db and recompute when the value of the field changes.

Please refer this tutorial:

https://www.cybrosys.com/odoo-development-tutorial/v14/computed-fields-api-depends/

Regards

Avatar
Discard
Related Posts Replies Views Activity
1
Sep 23
7000
3
Apr 24
1706
0
Jan 24
1255
2
Jun 23
3447
1
Apr 23
3065