Don't get records created in One2many onchange field in transient model
I am trying to create single field records in one of my transient models when exchanging a boolean field.
Eg.
Models
class test_model(models.TransientModel):
_name ="test.model"
is_okay = fields.Boolean("Okay?")
lines = fields.One2many("opposite.model","test_id",string="Lines")
@api.onchange('is_okay')
def onchnage_is_okay(self):
ids = []
for l in range(5):
record = self.env['opposite.model'].create({'name':str(l),'test_id':self.id})
ids.append(record.id)
self.lines = [(6,0,ids)]
class opposite_model(models.TransientModel):
_name ="opposite.model"
name = fields.Char("Name")
test_id = fields.Many2one("test.model",string="Test Model")
View
<record id="view_form" model="ir.ui.view">
<field name="name">view.form</field>
<field name="model">test.model</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Test Model">
<field name="is_okay" />
<field name="lines" />
<footer>
<button name ="click_okay" string="Okay" type="object"/>
</footer>
</form>
</field>
</record>
Now the problem is that when checking or unchecking the is_okay checkbox, it populates the records in the One2many field.
This works fine.
But in my view above, I have a button that calls a method click_okay()
.
Eg.
@api.one
def click_okay(self):
print self.lines
So the print statement gives me an empty recordset. But, I can see 5 records in the view when I change the is_okay field.
I don't understand how to get these lines in a method?
Any answer would be appreciated?
source to share
It should work. This is wired behavior.
You can try the following alternative way using self.update ()
@api.onchange('is_okay')
def onchnage_is_okay(self):
ids = []
for l in range(5):
record = self.env['opposite.model'].create({'name':str(l),'test_id':self.id})
ids.append(record.id)
self.update({
'lines' : [(6,0,ids)]
)}
source to share
Regardless of what odoo continues to do the same:
the problem is that odoo always passes these records to create a method using a command 1
, but in odoo we cannot use this command in the create method and this is why you lose these records when you call the method.
(1, id, values) updates an existing id record id with values ββin values. Cannot be used in create ().
I don't know why you are creating this entry in the event onchange
, because it is not recommended if the user types in a target and not ok, the entry is completely ready to be created in the database and every time he checks the button, it recreates that entry again and again.
if you don't need to create these entries in the onchange event, what you need to do is:
@api.onchange('is_okay')
def onchnage_is_okay(self):
ids = []
for l in range(5):
record = self.env['opposite.model'].new({'name': str(l)})
ids.append(record.id)
self.lines = ids
one thing here onchange will return the dictionnary in the form, the one2 field tree must have all the fields that are passed in this dictionary, in this case the tree must have a field name
if it oppisite.model
has, for example, another field for example test_field
if we go {'name': value, 'test_field': value_2}
through if the tree only has a name
value fields test_field
will be lost in the creation method.
but if you need it you have to work arround odoo and change the command to 4 in method creation:
@api.model
def create(self, vals):
"""
"""
lines = vals.get('lines', False)
if lines:
for line in lines:
line[0] = 4
line[2] = False
vals.update({'lines': lines})
return super(test_model, self).create(vals)
source to share