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

how to pass records from list to many2many field?

Subscribe

Get notified when there's activity on this post

This question has been flagged
listmany2manypython3arrayodoo12.0
12 Replies
20039 Views
Avatar
Mohamed Fouad (personal)


 need to get the range between 2 years on a many2many field each year must be a record on the model

i added 2 fields to get the 2 years and and method to get the range between them

class yearrange(models.Model):
_name = 'yearrange'
_rec_name = 'name'

name = fields.Char()

 class autopart(models.Model):
_inherit = 'product.template'

@api.multi
@api.depends('start', 'end')
def years(self):
    record = [int(x) for x in range(int(self.start), int(self.end))]
    for rec in self:
        rec.rang=record

start = fields.Char(string="", required=False, )
end = fields.Char(string="", required=False, )
rang = fields.Many2many(comodel_name="yearrange", string="",compute=years )

i get records at the many2many field but all of them are = False 

0
Avatar
Discard
Sehrish

In this article I will show you how to create a many2many field in odoo. I will also show you guys how to filter many2many field using domain. You can also learn how to set default value on many2many field.

Reference: https://learnopenerp.blogspot.com/2018/12/add-domain-on-many2many-field-in-odoo.html

Mohamed Fouad (personal)
Author

i solve it by this code

class yearrange(models.Model):

_name = 'yearrange'

_rec_name = 'name'

name = fields.Char()

product_id = fields.Many2one(comodel_name="product.template")

@api.multi

@api.onchange('start', 'end')

def years(self):

print('innnnn')

for rec in self:

if rec.start and rec.end:

record = [int(x) for x in range(int(rec.start), int(rec.end) + 1)]

list = []

for item in record:

print(item)

range_id = self.env['yearrange'].create({

'name': str(item)

})

list.append(range_id.id)

rec.rang = [(4, x, None) for x in list]

start = fields.Char(string="", required=False, )

end = fields.Char(string="", required=False, )

rang = fields.One2many(comodel_name="yearrange", inverse_name="product_id", store=True, string="range")

Avatar
Mohamed Fouad (personal)
Author Best Answer


i solve it by this code

class yearrange(models.Model):
_name = 'yearrange'
_rec_name = 'name'

name = fields.Char()
product_id = fields.Many2one(comodel_name="product.template")


class autopart(models.Model):
_inherit = 'product.template'

@api.multi
@api.onchange('start', 'end')
def years(self):
print('innnnn')
for rec in self:
if rec.start and rec.end:
record = [int(x) for x in range(int(rec.start)+1, int(rec.end)+1)]
list = []
for item in record:
print(item)
range_id = self.env['yearrange'].create({
'name': str(item)
})
list.append(range_id.id)
rec.rang = [(4, x, None) for x in list]

start = fields.Char(string="", required=False, )
end = fields.Char(string="", required=False, )
rang = fields.One2many(comodel_name="yearrange", inverse_name="product_id", store=True)



1
Avatar
Discard
Avatar
Mital Vaghani
Best Answer

Hello,

​rang is a Many2many field with model yearrange and it's functional field, so in computed method of that field you need to give ids  of yearrange model and you are assigning direct years.
So you can write as following :

@api.multi
@api.depends('start', 'end')
def years(self):
    record = [str(x) for x in range(int(self.start), int(self.end))]
    for rec in self:
        yearranges = self.env['yearrange'].search([('name','in',record)])
        rec.rang=yearranges

0
Avatar
Discard
Mohamed Fouad (personal)
Author

Thanks dear for your help i appreciate that too much but it's still not working for me

the field range doesn't updated and i added print (rec.rang) its print yearrange()

not any record

Avatar
Manish Bohra
Best Answer


Hello,
In your case your  Many2many field range are used as functional filed(Compute field) So in this case you want to update your id in respective field.

So you following changes in your code maybe it's works for you

@api.multi
@api.depends('start', 'end')
def years(self):
    record = [str(x) for x in range(int(self.start), int(self.end))]
    for rec in self:
        yearranges = self.env['yearrange'].search([('name','in',record)]).ids
        OR
      yearranges = self.env['yearrange].search([('name','in',record)])
     rec.rang=yearranges
​
Thanks 

0
Avatar
Discard
Mohamed Fouad (personal)
Author

Thanks dear for your help i appreciate that too much but it's still not working for me

the field range doesn't updated and i added print (rec.rang) its print yearrange()

not any record

Manish Bohra

can you share the value of record?

Mohamed Fouad (personal)
Author

first solution rec and yearranges isn't used

if i added rec to rec.yearranges and print it display empty list []

second solution i get error

File "/odoo/odoo-server/odoo/osv/expression.py", line 318, in distribute_not

elif token in DOMAIN_OPERATORS_NEGATION:

TypeError: unhashable type: 'list'

Manish Bohra

ok, but what is the output of the record variable?

Mohamed Fouad (personal)
Author

it print list of range like >>>>>> ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019']

Manish Bohra

What is value in name ..? Similar like year ..?

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
Related Posts Replies Views Activity
[Odoo12] Check current model in python Solved
python3 odoo12.0
Avatar
Avatar
Avatar
2
Feb 20
13328
How to replace previous references in One2Many Field
python3 odoo12.0
Avatar
Avatar
1
Jul 19
5304
TypeError: unhashable type: 'list'
list many2many
Avatar
Avatar
Avatar
Avatar
4
Mar 15
34108
Can't add items to Many2many in Odoo 12 EE
enterprise many2many odoo12.0
Avatar
Avatar
Avatar
Avatar
4
Feb 19
8369
unhashable type: 'list' -many2many
list many2many unhashable
Avatar
1
Dec 17
7030
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