This question has been flagged
5 Replies
3264 Views

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 

Avatar
Discard
Best Answer

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)]"/> 


Avatar
Discard
Author

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: )>"

Author

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.

Author

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.

Author

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

Author

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?

Author

yep, to fill it automatically

Best Answer

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

Avatar
Discard
Author

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