Skip to Content
Menu
This question has been flagged
1 Reply
1117 Views

creating a function to set default value of a Many2one field (thanks 2 @Kiran Mohan), odoo says NameError: name 'default_categry' is not defined , please help, using Pycharm as IDE, screenshot is here:


regards

Avatar
Discard
Best Answer

Hi,

    1. Usually .ids is used only for returning ids to many2many fields and not for  many2one field.

    2. default works when a record is newly created , when you select create button  to  create a form or when you select add a line , It works on the go. 
    3. Here on the go, there is no data filled in items_id. Only if items_id is available you can check the category of the item.

    3. So i guess default wont work in your case.

                                                            or

 you can have your categry_id  field as a related one like,

categry_id = fields.Many2one('product.category', string='Category', related='items_id.categ_id', store=True)

Related fields wont be stored in database unless you give store = True

                                                    or

Try api.onchange

@api.onchange('items_id')

def onchange_items_id(self):

   if self.items_id:
       self.categry_id = self.items_id.categ_id and self.items_id.categ_id or False
    else:

        self.categry_id = False 

Hope it helps,

Thanks

Avatar
Discard
Author

thanks @Karthikeyan, for feedback and to help other through your Answer, i have to mention that your code below works fine... :)

categry_id = fields.Many2one('product.category', string='Category', related='items_id.categ_id', store=True)

Hi,

For you information, if you change the category of that product in product category master, then in your transaction form, the category of the product will get changed.

I suggest you to make sure you don't change anything in the reference field (category) when it is used as a related field.

If this is your requirement ( If any change made in master to be reflected in all of your old transactions then you can use related in your development.)

So i always suggest you to go with the last one....api.onchange

Thanks

Related Posts Replies Views Activity
2
Apr 23
1452
2
May 24
35813
2
Nov 22
1478
1
Feb 22
6687
2
Dec 21
2706