Reputation: 4174
I need to delete one2many records in onchange method. I have tried below code.
*.py
@api.onchange('abt')
def _onchange_active(self):
resident_flat_rel_id =[]
if self.active == False:
write=self.write({'resident_flat_rel_ids':[(2,self.resident_flat_rel_ids.id,False)]})
print'werite', write // prints True.
But record doesn't delete from database.
resident_flat_rel_ids
is one2many field in parent model.
Upvotes: 0
Views: 12942
Reputation: 11
You can simply remove one2many records using following one2many rule: (2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
return {'value': {'resident_flat_rel_ids':[(2,individual.id) for individual in self.resident_flat_rel_ids]}}
Upvotes: 1
Reputation: 1659
you can do one thing...
If you want to remove complete data do this:
Iterate over your one2many field, ie resident_flat_rel_ids Like
for individual in resident_flat_rel_ids:
if some condition: #If it's required
individual.unlink()
If you want to remove only the reference, then do this: Make the inverse_name (A many2one field which represents a reverse relation for the one2many field) of every object to False
for individual in resident_flat_rel_ids:
if some condition: #If it's required
individual.inverse_name = False
Upvotes: 0
Reputation: 769
Actually one2many
is not stored in db. ORM is making one2many
for you from the related many2one
fields. Once we know that we can proceed to delete record.
Ok now we know how one2many
works and we need to remove record. Here is two options: remove record from db or remove only reference from one2many
field.
For removing record you should delete it (see official documentation for this. hint: unlink()). And if you want to delete just reference then you should erase related record's many2one
field, simple enough.
Good luck
Upvotes: 3