Good day,
I have written a record rule just as below
domain=['|',('type', '!=', 'private'),('is_prescription', '=', True)])
Am using it in a variable just as below
`product_id = fields.Many2one('product.product', ondelete="cascade", string='Product', required=True, domain=['|',('type', '!=', 'private'),('is_prescription', '=', True)])`
To break it all down
The ('|',('type', '!=', 'private'),('type', '=', False)) helps me to bypass the default rule for multi company products.
And the record rule ('is_prescription', '=', True) helps me to get a specific product.
But the rule domain=['|','|',('type', '!=', 'private'),('type', '=', False),('is_prescription', '=', True)] is not helping to get a specific product that is a prescription, what it does it get all the products.
What i want to achieve is to bypass the default rule then get a product that is Prescription no matter what company the product belongs too and the user is in.
Please help me, I am new to Odoo
Put your prescription rule with "AND-condition" with other rule instead of "OR-condition"
domain=[('is_prescription', '=', True),'|',('type', '!=', 'private'),('type', '=', False)]
Related
I am trying to make a button in an Odoo view invisible when two conditions are both met.I am able to make the field invisible when either of the conditions are met like this:
attrs="{'invisible': [('status', '!=', 'validated'),('api_result', '!=', 'Valid')]}"
I want the button to be visible only when status == "validated" && api_result == "Valid"
But when I try to introduce the "&" I get an error. There's a long traceback which I'm not attaching now because I assume there is something wrong with my syntax:
attrs="{'invisible': ['&',('status', '!=', 'validated'),('api_result', '!=', 'Valid')]}"
I admit that I find the syntax 'challenging' and haven't been able to find any clear documentation, so I would appreciate a pointer.
You can do that using or as below:
attrs="{'invisible': ['|',('status', '!=', 'validated'),('api_result', '!=', 'Valid')]}"
The pos module extends product.template and adds available_in_pos field. In a select field to choose a product, I would like to filter only products available in pos.
I tried the domain [('product_tmpl_id.available_in_pos', '=', True)] but I get this error
Unknown field "product.template.available_in_pos" in domain of <field name="product_id"> ([('product_tmpl_id.available_in_pos', '=', True)]))
Anyone knows how I achieve this?
There are two models for product, first one is product.template which is the template for product and second one is product.product which inherits all data structure of product.template and this type of inheritance called delegation inheritance.
One of the reasons to use two models for product is using product variants, You will find one product template but many product variants for same product template which stored in product.product
The pos module extends product.template and adds available_in_pos field so this field will be available in product.product model because of the delegation inheritance.
So, you filter only products available in pos by using this domain [('available_in_pos', '=', True)]
Examples:
If you are using domain with product_id field which belong to
product.template model, you can filter only products available
in pos with the following domain [('available_in_pos', '=', True)]:
product_template = self.env['product.template'].search([('available_in_pos', '=', True)])
If you are using domain with product_id field which belong to
product.product model, you can filter all products (include
products variants) with following domain [('available_in_pos', '=', True)] or you can filter it using their product template uing this domain [('product_tmpl_id.available_in_pos', '=', True)]
product = self.env['product.product'].search([('available_in_pos', '=', True)])
or
product = self.env['product.product'].search([('product_tmpl_id.available_in_pos', '=', True)])
I want to make invisible a field if one of those options are true, I don't know if this is possible.
I try:
<field name="x_field1" string="something" attrs="{'invisible': [('x_field2','!=','value'),'|',('x_field3','=','value'),'|',('x_field4','=','value')]}"/>
And this:
<field name="x_field1" string="something" attrs="{'invisible': ['|',('x_field2','!=','value'),('x_field3','=','value'),('x_field4','=','value')]}"/>
Without success.
From Odoo Domains documentation:
'|'logical OR, arity 2.
You have three options, so you need to use two | operators, like the following:
['|', '|', ('x_field2', '!=', 'value'), ('x_field3', '=', 'value'), ('x_field4', '=', 'value')]
When I edit a record from this field (code below), it doesn't save for some reason. It's a computed field, linking to res.partner records. If I edit it and click save, it doesn't save at all (no changes in the database and/or if I hard refresh the page). Does someone see something here that I'm missing? If I can't edit it via what I'm expecting, is there another way to do this? The reason I do a computed field and not a domain on child_ids is because child_ids field with a domain doesn't seem to work properly with this domain.
Model
contact_ids = fields.One2many(comodel_name='res.partner', compute="_get_contact_ids", readonly=False)
#api.multi
#api.depends('child_ids')
def _get_contact_ids(self):
for company in self:
if company.child_ids:
company.contact_ids = company.child_ids.search([('is_location', '=', False), ('parent_id', '=', company.id), ('type', '=', 'contact')])
View
<field name="contact_ids" string="Contacts">
<tree create="true" delete="false" edit="true" default_order="create_date">
<field name="name"/>
<field name="phone"/>
<field name="email"/>
</tree>
</field>
Update
Added this per ideas, but it didn't work. Keep in mind, this is on a model that inherits res.partner.
activity_contact_id = fields.Many2one('res.partner', string="Contact")
contact_ids = fields.One2many(
comodel_name='res.partner',
inverse_name='activity_contact_id',
compute="_get_contact_ids",
readonly=False,
stored=True
)
Computed fields in Odoo are not stored by default, you need to set store=True in order to save the fields to database.
contact_ids = fields.One2many(comodel_name='res.partner', compute="_get_contact_ids", stored=True, readonly=False)
To store a one2many value in database you need the inverse_name on the other model.
I mean that you need to create a many2one field to save the id of the current record
in the co_model. (o2m needs m2o you cannot store the values without m2o !! remember this role)
don't use one2many field use many2many field it is better.
contact_ids = fields.Many2many(comodel_name='res.partner',
relation="your_model_res_partner_rel", # always mention the name of the relation good practice
column1 = "you_mode_id",
column2 = "partner_id",
compute="_get_contact_ids",
store=True) # make your field stored no need for readonly it's by default
#api.depends('child_ids')
def _get_contact_ids(self):
""" always explain what the method do here good practice for team work"""
for company in self:
if company.child_ids:
# break you line when it's to long to be readable
ids = company.child_ids.search([('is_location', '=', False),
('parent_id', '=', company.id),
('type', '=', 'contact')]).ids
company.contact_ids = [(6, False, ids)] # replace all records by the new ids
In my module I want to filter one2many records based on current date.
This is my xml code
<field name="record_ids" domain="[('end_date', '>', cur_date)]">
<tree string="records_tree">
<field name="record_id"/>
<field name="record"/>
<field name="start_date"/>
<field name="end_date"/>
</tree>
</field>
cur_date is a functional field I added to get current date.
My problem is records are not filtered in the view. Also it doesn't show any error message
you are define the domain in the XML file .
so this domain it's not work .
please define in the .py file .
For Example :
'record_ids':fields.one2many('model_name','model_id','Record',domain=[('end_date', '>=', 'cur_date')])
here the cur_date you need to define one function field which show the current date.
So Please check this may be it's help full to you :).
I also faced this problem, and the solution is put domain in .py file, in .xml domain is not working properly.
import_transaction_log_ids = fields.One2many(comodel_name = 'transaction.log','sale_order_id', string = 'Import Transaction Log',domain=[('operation_type','=','import')])
in example operation_type field is in transaction.log model.
Write domain in end_date field, like this:
<field name="record_ids" >
<tree string="records_tree">
<field name="record_id"/>
<field name="record"/>
<field name="start_date"/>
<field name="end_date" domain="[('end_date', '>', cur_date)]"/>
</tree>
</field>
i think it will help you..
you can pass only those field in domain those are store in Database.
So in that case cur_date is not store in Database.
Then also you need to pass into domain so you need to store cur_date field from py.
first of all, one2many fields are not for selection purpose. We can create the new records or update the existing records in one2many field. so we cannot apply domain to a one2many field.
eg: sale_order_line field in sale.order
moreover one2many fields, functional_fields [**if store=True not specified ] wont store in the table.
Many2one or Many2Many are used for selecting the records [ as well as creating new ones ], so here we can apply domain and we can restrict the user to select some type of records
eg: Many2one- product_id field in sale.order.line
many2many - user_ids field in res.users
So, in order to get your task, try many2many and apply domain, then the records will be filtered
domain contains 'field name' 'expression' 'value'.
instead of value you given a field
<field name="record_ids" domain="[('field', 'expression', value)]">
Add it in python:
Eg:
xn_cutting_ids = fields.One2many('mrp.bom.line', 'bom_id', 'Cutting Lines', domain=lambda self:[('xn_stage','=','cut')])
Use domain = lambda else there is a chance of error while using string values in domain.
Here xn_stage is in mrp.bom.line model.
On Odoo V11
Define a function that returns a domain in the one2many field definition.
class GroupContract(models.Model):
_name = 'group.contract'
#api.multi
def _domain_move_ids(self):
"""Odoo default domain for many2one field is [('contract_id', '=', self.id)].
This function adds new criteria according to your needs"""
res = []
if len(self) == 1: # do not compute in tree view
ids = self.env['stock.move'].search([
('state', '=', 'done'),
('date', '>=', self.start_date),
('date', '<=', self.end_date)
]).ids # choose your own criteria
res = [('id', 'in', ids)]
return res
start_date = fields.Date(string="Start date", required=True)
end_date = fields.Date(string="End date", required=True)
move_ids = fields.One2many(comodel_name='stock.move', inverse_name='contract_id', string="Moves",
domain=lambda self: self._domain_move_ids())