Skip to Content
Menu
This question has been flagged

In version v9.x I have defined models that have One2many and Many2one relation:

Sample code, that doesn't works for me (created in new module):

class ClassA(models.Model):    
    _name = 'module.class_a'    
    
    CYCLE = 'cycle'   
    AMOUNT = 'amount'   
    Other = (        
        (CYCLE, 'Cycle'),        
        (AMOUNT, 'Amount')    
    )    
    name = fields.Char(string='Name', required=True)   
    other_influence = fields.Selection(Other, string='Other influence', required=True)   
    related = fields.One2many('module.class_b', 'components')   
    
    @api.onchange('other_influence')    
    def _other_influence_change(self):        
        self.related = None       
        if self.other_influence == self.CYCLE:            
            b = {               
                'name': 'XXX', # Lets say we do generate something here, hardocded just to show the problem
            }           
            self.related += self.related.new(b)
    
class ClassB(models.Model):   
    _name = 'module.class_b'   
    
    name = fields.Char(string='Name', required=True)   
    components = fields.Many2one('module.class_a', ondelete='no action')

When user create new entry for ClassA in view I would expect when clicking on Other influence dropdown and select Cycle in new XXX will appear in related, however what happen is that a new blank line appear, and when trying to save it ends up with error.

openerp.sql_db: bad query:  SELECT "module_class_b"."components" as "components","module_class_b"."id" as "id" FROM "module_class_b"
                        WHERE "module_class_b".id IN ('one2many_v_id_53')  ORDER BY "module_class_b"."id"

I know the difference betweem new and create, but if I change new to create in line

self.related += self.related.create(b) 

and click at Other influence field view works fine and new row is added, e.g. Name field contains XXX. Save works as well, but this is obviously bad setup as each click on Other influence would create and entry in DB (but works, both updated view and also store, but create shouldn't be used here instead on new).

I would like to click at Other influence field in view function _other_influence_change is triggered with

self.related += self.related.new(b)

and new row is added in view, and it shows data from variable b, eg. Name field contains XXX. And save to the DB successfully once user pres the save button.

I have tried to look for existing bugs and also advises how to do it properly but nothing doesn't seems to work, colleague of mine told me that this has worked fine in v8.x. Thanks for the advises.

Avatar
Discard
Related Posts Replies Views Activity
0
Nov 22
132
2
Aug 20
2370
0
Mar 15
3006
1
Jan 24
695
1
Oct 23
3981