Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

Removing elements from many2many field

Subscribe

Get notified when there's activity on this post

This question has been flagged
1 Reply
42540 Views
Avatar
Zakaria

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. 



1
Avatar
Discard
Avatar
Yenthe Van Ginneken (Mainframe Monkey)
Best Answer

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

4
Avatar
Discard
Zakaria
Author

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

Yenthe Van Ginneken (Mainframe Monkey)

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.

Abhijith soman

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

Eldhose George

can you please mention an example

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now