Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

api.onchange modification One2many field with NewItem does not works

By
Richard Kellner
on 3/8/16, 4:19 AM 394 views

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.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 3/8/16, 4:19 AM
Seen: 394 times
Last updated: 3/8/16, 4:46 AM