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 filter field on a one2many?

Subscribe

Get notified when there's activity on this post

This question has been flagged
2 Replies
10351 Views
Avatar
Anirudh Lou

Hello everyone, kindda lost for my odoo adventure and i need a help. I have a class named "planned.bom", some of it's columns are product_id (many2one on product), project_id (many2one on project). This is where i stuck, in my purchase requisition, when i add purchase requisition lines, i want to show only those products which are under planned.bom and base on project.

Here is the image i want to tell:

Any help is much appreciated.

 

 

-------------------------------------------------------------------------

 

In purchase.requisition i add fields.function;

'product_ids_to_chose_from' : fields.function(_get_assigned_bom, type='many2many', method=True,store=False, multi='compute_assigned_bomproduct', string='Assigned BOM product')

and the functions looks lik this:

 

    def _get_assigned_bom(self,cr,uid,ids,context=None):
        vals = {}
        pr_id = None
        for rec in self.browse(cr, uid, ids, context=context):
            pr_id = self.pool.get('project.project').search(cr,uid,[('id','=',rec.project_id.id)])
            for project in self.pool.get('project.project').browse(cr,uid,pr_id,context=context):
                p_bom_ids = self.pool.get('planned.bom').search(cr,uid,[('project_id','=',project.id)])
                for bom in self.pool.get('planned.bom').browse(cr,uid,p_bom_ids,context=context):
                    pr_id = bom.product_id.id
            vals[rec.id] = {'product_ids_to_chose_from':pr_id}
        return vals

 

on purchase.requisition.line i add this one:

'product_ids_to_chose_from': fields.related('requisition_id', 'product_ids_to_chose_from', type='many2many', string='Assigned BOM product'),

and on my form here how create the domain:

<page string="Products" attrs="{'invisible':[('state','=','new')]}">
      <field name="line_ids" context="{'requisition_id': active_id}" attrs="{'readonly':[('state','not in','add_product')]}">
                 <tree string="Products" editable="bottom">
                       <field name="product_id" required="True"                                               

                                               on_change="onchange_product_id_limit(product_id,product_uom_id,product_qty,context)"

                                              domain="[('is_bom','=',True),('ids','in',product_ids_to_chose_from[0][2])]" />

upon clicking on a product i recieve this client error:

Uncaught Error: NameError: name 'product_ids_to_chose_from' is not defined

 

i don't know what it cause.

 

****************** EDIT

 

    def _get_assigned_bom(self, cr, uid, ids, name, arg, context=None):
        vals = {}
        pr_id = []
        for rec in self.browse(cr, uid, ids, context=context):
            id = rec.id
            vals[id] = []
            if rec.project_id:
                p_bom_ids = self.pool.get('planned.bom').search(cr,uid,[('project_id','=',rec.project_id.id)])
                for bom in self.pool.get('planned.bom').browse(cr,uid,p_bom_ids,context=context):
                    pr_id = bom.product_id
            vals[id] = [(6, 0, [x.id for x in pr_id])]
        return vals

1
Avatar
Discard
Avatar
Ivan
Best Answer

I presume that the purchase.requisition (PR) is somehow linked to the planned.bom.  So, here is a trick that I usually used:

  • Create a function many2many field in the PR (or you can alternatively populate this column during create / write).
  • The many2many field need to be populated with all the product.product that is linked to the planned.bom.
  • In your purchase.requisiton.line model, extend (using related field in v7 or compute argument in v8) to reflect the new many2many field of the related PR.  Say, it is named product_ids_to_chose_from
  • Use that field to domain your product_id: domain="[('ids', 'in', product_ids_to_chose_from[0][2])]"
2
Avatar
Discard
Anirudh Lou
Author

Thank you sir for your response, actually purchase.requisition has no direct link with planned.bom other than that it has many2one on project.project and with all due respect sir, do you mean in 'create a function' i need to create fields.function on my PR?

Ivan

It's OK if the link is not direct as long as it has link. Because it is needed for the step in the 2nd point. And yes, you need to create fields.function (I presume that you are developing in v7 then, cause in v8 it is implemented differently).

Anirudh Lou
Author

Sir i updated my question above, i am not sure if i correctly followed your suggestion and that's why i got an error.

Ivan

The domain need to be implemented in the view XML not from the field definition. Reason: product_ids_to_chose_from can only have value when a record is selected. The domain in field definition can only use something that is not depending on the record value.

Ivan

One more note: the first 3 lines in the first for loop in _get_assigned_bom can be collapsed. There are 3 lines that I'm referring to: pr_id = self.pool.get('project.project').search(cr,uid,[('id','=',rec.project_id.id)]); for project in self.pool.get('project.project').browse(cr,uid,pr_id,context=context):; p_bom_ids = self.pool.get('planned.bom').search(cr,uid,[('project_id','=',project.id)]). Can be collapsed to p_bom_ids = self.pool.get('planned.bom').search(cr,uid,[('project_id','=',res.project_id.id)]). You might want to put a check before to see if res.project_id is empty or not.

Anirudh Lou
Author

Sir, i guess i recieved a client error because my fields product_ids_to_chose_from is not srored in my database, I am not sure though. Yeah the domain: "domain="[('is_bom','=',True),('ids','in',product_ids_to_chose_from[0][2])]" is in my view form. I tried to make product_ids_to_chose_from as store=True but it also produce an error.

Ivan

Sorry forgot to mention that you need to put the field product_ids_to_chose_from in the view. Don't store, unless if you are sure that the list of products will only change if the purchase.requisition is saved (i.e. there is no way that the list of products may change because there is new planned.bom added to the project.)

Anirudh Lou
Author

Sir, after adding the field on the view, my using vals[rec.id] = {'product_ids_to_chose_from':pr_id} produce no output, when i click the tree view of my purchase requisition it says: "Uncaught TypeError: undefined is not a function" and using vals[rec.id] = pr_id it says: "AttributeError: 'int' object has no attribute 'iteritems'"

Ivan

Return of _get_assigned_bom should be in the form or [(6, 0, [XXXX])]. Where [XXXX] is the list of product ids that you have collected. I see that you assign just one id to pr_id. Why so? Also, pr_id variable is reused, you may end up in the situation where self.pool.get('planned.bom').browse(cr,uid,p_bom_ids,context=context) and your pr_id is the one returned from self.pool.get('project.project').search(cr,uid,[('id','=',rec.project_id.id)]).

Anirudh Lou
Author

Do you mean sir, pr_id = bom.product_id.id must be changed to pr_id = bom.product_id and then loop for this items? Or my head is already spinning and that's why i can't spot it.

Anirudh Lou
Author

Sir I have updated my function replacing vals[rec.id] into vals[id] = [(6, 0, [x.id for x in pr_id])] and terminal says, NotImplementedError: Iteration is not allowed on browse_record(product.product, 4)

Ivan

Because you are still using pr_id which is assigned in this line: pr_id = bom.product_id. And thus pr_id is a single browser_record and cannot be interated. You should collect all bom.product_id in a list, like so: pr_id = [bom.product.id if bom.product else False for bom in self.pool.get('planned.bom').browse(cr,uid,p_bom_ids,context=context)]

Anirudh Lou
Author

Oh oh, kindda nose bleed for the code structure. Can you explain it to me sir, if it's okay with you.

Anirudh Lou
Author

Sir, using vals[id] = [(6, 0, [x.id for x in pr_id])] produce an error, it says AttributeError: 'int' object has no attribute 'id'. When i tried, vals[id] = {'product_ids_to_chose_from':pr_id} it works but on my purchase.requisition.line field "product_ids_to_chose_from" is empty while field "product_ids_to_chose_from" on purchase.requisition has item on it. When i check product_ids_to_chose_from on purchase.requisition.line it is related to product_ids_to_chose_from on purchase.requisition.

Ivan

Sorry, I did make the pr_id to be a list of Database IDs instead of list of browse_objects. You can change the vals[id] line to be: vals[id] = [(6, 0, pr_id)] OR change the pr_id line to be: pr_id = [bom.product for bom in self.pool.get('planned.bom').browse(cr,uid,p_bom_ids,context=context)]

Ivan

And if you have difficulty in reading the code, I suggest that you familiarise yourself with list comprehension: https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions, http://www.python-course.eu/list_comprehension.php, http://www.secnetix.de/olli/Python/list_comprehensions.hawk

Anirudh Lou
Author

I have tried it sir, using vals[id] = [(6, 0, pr_id)] and it says AttributeError: 'list' object has no attribute 'iteritems'.

Ivan

Not sure what causes the AttributeError. But from code I think vals has been defined as list instead of dictionary as it should. The code that you have posted in the question defined it as dictionary.

Anirudh Lou
Author

but everytime i tried vals[id] = {'product_ids_to_chose_from':pr_id} it works but on the purchase.requisition only, on purchase.requisition.line the field product_ids_to_chose_from is empty.

Anirudh Lou
Author

but everytime i tried vals[id] = {'product_ids_to_chose_from':pr_id} it works but on the purchase.requisition only, on purchase.requisition.line the field product_ids_to_chose_from is empty.

Ivan

It is from a different method. Your definition of vals does not carry over different methods.

Anirudh Lou
Author

even if it is fields.related sir?

Anirudh Lou
Author

After changing vals = {} into vals = [] and assigning vals[id] = [(6, 0, pr_id)], now the compiler says: "IndexError: list assignment index out of range" for my vals assignment.

Anirudh Lou
Author

Sir,can i ask one more question. Now in my purchase requisition form it does not completely shows my BOM (i.g if project A, have 4 BOM only three is bieng displayed on my form. I tried to show it on my terminal but it all print planned.bom under that project). What's the possible cause of this one?

Ivan

Sorry I did not caught your earlier messages. I'm not sure what you mean by your question about fields.related. The vals need to be a dictionary {} not list []. The error "IndexError: list assignment index out of range" is due to you using a list. On the subject of 4 BOMs showing only 3, I would suggest that you check: 1. Are there really 4 planned.bom that are connected to that project (whose project_id is the project's database ID)? 2. Do you have any Record Rules or any domain applied to the view/column/model?

Anirudh Lou
Author

In my purchase.requisition.line i have product_ids_to_chose_from w/c is of fields.related on product_ids_to_chose_from from purchase.requisition. For number one, yes there exist a four planned bom under same project. For number 2, yes there is a domain on product_ids_to_chose_from in my view.

Anirudh Lou
Author

Actually sir, it works properly on my purchase.requisition.line, it shows all products whose planned bom is under project selected by the user from purchase.requisition. I am just wondering, why all planned bom are not shown on purchase.requisition view.

Ivan

If it shows in the purchase.requisition.line but not in the purhcase.requisition, my suspect will be a domain that prevents the record to be shown in the view definition.

Anirudh Lou
Author

Copy, perhaps i just have to make it invisible. What is the important is it filter the product. By the way thanks a lot sir for your patient especially for the help. :)

Avatar
Anirudh Lou
Author Best Answer

Thanks for the links sir.

0
Avatar
Discard
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