This question has been flagged
5 Replies
16478 Views

Here i try to update users value to one2many field (incentive_users_ids),but  only last  user value is updating ,not updating all the users details. i am getting below outupt

https://prnt.sc/h46n5u

(only one user listing) ,i have 5 users in user list




class IncentiveDetails(models.Model):

    _name='incentive.details'

to_date=fields.Date('To')

incentive_users_ids=fields.One2many('incentive.users','incentive_id','Incentive Details')

    

@api.onchange('to_date')

    def _onchange_date(self):

         result=[] ######updated code now its working

        for all_users in self.env['res.users'].search([]):

              

result.append((0,0,{'sales_user_id':all_users.id}))

for record in self:
record.incentive_users_ids=result

class IncentiveUsers(models.Model):

    _name='incentive.users'

    incentive_id=fields.Many2one('incentive.details')

    sales_user_id=fields.Many2one('res.users','Sales Person')

Avatar
Discard

If you give result=[] inside the loop, how do you expect to get all users value appended to your list?

Author

yes.. correct

Best Answer

Updating the one2many and many2many fields have some rules, we need to add some flags for such operations.

(0, 0, { values }) link to a new record that needs to be created with the given values dictionary

(1, ID, { values }) update the linked record with id = ID (write values on it)

(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)

(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)

(4, ID) link to existing record with id = ID (adds a relationship)

(5) unlink all (like using (3,ID) for all linked records)

(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)


you can check more details in here


Avatar
Discard
Best Answer

I have a link that can help you to updating many values for One2many field.

https://stackoverflow.com/questions/20954412/create-and-edit-items-of-a-one2many-field-through-on-change-method

Avatar
Discard
Best Answer

Are you on V10 ?

Avatar
Discard