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

i have two models crm.store in which i have declared thoses two fields

name = fields.Char(string="Name store")
id_store = fields.Char(string="ID store")

and  crm.unit which in which i have the fallowing fields 

st_or_id = fields.Char(string="ID")
store = fields.Many2one('crm.store', string='Store')
store_id = fields.Char(related='store.id_store', string='Store ID)

i want when i give the st_or_id, the store field will be feed up with the store name that has the same id of id_store 

for that i tried in my xml

    <field name="store_id " invisible="1"/>    
    <field name="st_or_id "/>
    <field name="store" domain="[('st_or_id ', '=', store_id)]"/> 

i want to know how could i acheive this, using an onchange function.

i tried that , to applie the domain on the store field

 @api.onchange('st_or_id')
def onchange_store_id(self):
    return {'domain': {'store': [('st_or_id', '=', 'store_id')]}}

but it dosent work 

形象
丢弃
最佳答案

I think you want to show Store (in m2o) matching entered ID (in st_or_id field) with "ID Store". If yes, you don't need onchange because you can directly add the domain in XML.

Try the following code:

<field name="store" domain="[('id_store', '=', st_or_id)]"/> 


形象
丢弃
编写者

hi

i tried that already, and i get this error

Invalid field 'magasin_id' in leaf "<osv.ExtendedLeaf: ('magasin_id', '=', '5577') on crm_magasin (ctx: )>"

编写者

magasin_id = is in my case id_store

As per error, "magasin_id" does not exist in "crm.magasin" object. Please double check the field.

编写者

yes i have checked, and i even declared the related field as store=True

It would be more helpful if you can share your original code.

编写者

i prefer to keep it as simple as the code that i am mentioning, the original code that i am working on, works in a three diffrent modules, so it just gonna make it even harder, i gave the same example with just renaming the fields for more clarity

编写者

besidesn your code is not gonna feed up automatically the store field based on the st_or_id, yours just established a domain where the id_store equals to st_or_id

Do you want to just apply a domain on the m2o field or want to fill it up automatically when you enter a value in "st_or_id" field?

编写者

yep, to fill it automatically

最佳答案

Hi,

Try this code,

@api.onchange('st_or_id')
def onchange_store_id(self):
for rec in self:
return {'domain': {'store': [('id', '=', rec.st_or_id)]}}

Thanks

形象
丢弃
编写者

it works like just a simple many2one field,not what i am looking for

相关帖文 回复 查看 活动
2
2月 23
4004
1
1月 21
5193
1
12月 19
2801
1
8月 24
2701
4
5月 24
12544