Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
41684 Widoki

Hello,

I would like to remove elements from many2many field. For example the field contining the ids : [1,2,3,4,5]

and we want remove the ids [1,2]  to obtain [3,4,5] in the final list.

This is  my complet code : 

class deplacement_packs(models.Model):

_name = 'affichage2.deplacement_packs'

pack_ref = fields.Many2many('affichage2.pack_stock', string="Numéro de série", required=True, domain=['|', ('vente', '=', None), ('approved_by', '=', None)])

pack_ref2 = fields.Many2many('affichage2.pack_stock', string="Numéro de série")

ferme_name = fields.Many2one('gestion_parcelles.ferme', string="Nom de ferme", required=False, ondelete='cascade')
date_affectation = fields.Char(string="Date de deplacement", required=False)
zone_text = fields.Text(string="Zone", required=False)

@api.multi
def deplacer_pack(self):

pack_ref2 = self.pack_ref

#liste de packs selectionées
listo = self.pack_ref.ids

#liste des packs de la ferme séléctionée
rec2 = self.env['gestion_parcelles.ferme'].search([('id', '=', self.ferme_name.id)])
listo2 = rec2.list_packs.ids

#ajouter les packs selectionées au packs de la ferme séléctionée
rec2.list_packs = listo

bb = filter(lambda x: x not in list(pack_ref2.ids), listo2)

self.zone_text = (self.pack_ref, rec2.list_packs, type(rec2.list_packs),bb, listo2)
self.date_affectation = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

#delete the modifable pack from the previous ferm ...
rec2.list_packs =(3,[pack_id for pack_id in self.pack_ref.ids])

The erreur is in the last line. 



Awatar
Odrzuć
Najlepsza odpowiedź

Hi Zakaria,

You can remove elements from a many2many field on a few ways. You can either delete a specific record and the relation if you know 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)

Or you can only delete the link to the remote record:

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

Or if you want to unlink all records (without deleting the remote record) on a many2many directly you can do it like this:

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

Or if you want to delete all records:

(6, 0, []) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)
because we use [] it'll drop all records.

For more information have a look at the official documentation at https://www.odoo.com/documentation/10.0/reference/orm.html

Regards,
Yenthe

Awatar
Odrzuć
Autor

Thank you so much Yenthe, i have tried that but i got this erreur :

The problem is that i get this erreur message :

The operation cannot be completed, probably due to the following:

- deletion: you may be trying to delete a record while other records still reference it

- creation/update: a mandatory field is not correctly set

Hmm, please add your code to the initial question then. This'll allow others to look at the code and see what is wrong, otherwise it is hard to judge.

Is it possible to remove the last added item in the many2many field

can you please mention an example