Skip to Content
Menú
This question has been flagged
2 Respostes
7731 Vistes

Hello Odoo community!
I write a function that calculates  day and then return today food ,how can i use this output as domain of product_id field?

column:

product_id = fields.Many2one('lunch.product', 'Product', required=True , domain="[???]")

my function:

def _menu(self):
"""
Prevents menu list
"""
day = self.env['lunch.menu'].search([]).mapped('day')
order_day = self.env['lunch.order'].search([]).mapped('date')
day_name = datetime.datetime.now().strftime('%A')
res={}
for order in self:
    for d in range (0,len(day)):
        if  day[d] == day_name:
            menu = self.env['lunch.menu'].search([('day' ,'=' ,day_name)]).mapped('product.id')
return menu



Avatar
Descartar

You must read this docs: Learn OpenERP is a great platform where you can specially learn OpenERP (odoo) and its programming concepts from A to Z. The purpose of creating this blog is to help those people who wants to develop their own OpenERP Systems. Have a look please:

https://goo.gl/8HgnCF

Best Answer

You can not use the result of methods in a domain, since the used domain criteria (field) should be present on a view.

However, you can compute field and place it to the form. Something like,

@api.multi
def _compute_menu_id(self):
for obj in self:
# the logic to define menu_id for this object
obj.menu_id = menu_id

product_id = fields.Many2one(
'lunch.product',
'Product',
required=True ,
domain=[("FOODFIELD", '=', menu_id)], #FOODFIELD should be a stored field of lunch.product field
)
menu_id = fields.Many2one(
"lunch.menu",
compute=_compute_menu_id,
Avatar
Descartar

I am trying to do something similar, and falling at the last hurdle.

I am trying to filter a Many2One field called attribute_type_id
I have added a One2Many field called valid_attrtype_ids
Using a compute method I am successfully populating valid_attrtype_ids with the set of records that I want to be able to select from. This field is visible, so I can see that it is populating correctly.

I am attempting to set a domain filter on attribute_type_id to only show the records that are in valid_attrtype_ids.

Here's my current syntax (I've tried this a few different ways):

<field name="attribute_type_id">
domain="[('id', 'in', valid_attrtype_ids)]"
</field>

I'm not getting any errors, however the field attribute_type_id is not being filtered. It is showing all records from the underlying model.

Any suggestions?

Best Answer

Hi,

This will helpful for you - \https://www.linkedin.com/pulse/how-set-dynamic-domain-filter-many2one-field-odoo-rishan-malaka?trk=portfolio_article-card_title

Avatar
Descartar
Related Posts Respostes Vistes Activitat
1
de jul. 18
9501
4
de nov. 20
7752
0
d’ag. 19
3303
3
de jul. 25
3775
0
de maig 25
692