Reputation: 1340
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
Upvotes: 6
Views: 13511
Reputation: 1
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())
Upvotes: 0
Reputation: 805
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.
Upvotes: 0
Reputation: 158
domain contains 'field name' 'expression' 'value'. instead of value you given a field
<field name="record_ids" domain="[('field', 'expression', value)]">
Upvotes: 0
Reputation: 4174
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..
Upvotes: 1
Reputation: 312
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.
Upvotes: 3
Reputation:
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 :).
Upvotes: 7
Reputation: 490
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
Upvotes: 0
Reputation: 1315
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.
Upvotes: 0