跳至内容
菜单
此问题已终结
2 回复
7726 查看

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



形象
丢弃

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

最佳答案

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,
形象
丢弃

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?

最佳答案

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

形象
丢弃
相关帖文 回复 查看 活动
1
7月 18
9500
4
11月 20
7747
0
8月 19
3297
3
5月 25
3773
0
5月 25
690