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

Set domain for one2Many for different views in Odoo12

Subscribe

Get notified when there's activity on this post

This question has been flagged
odoo12
14 Replies
9079 Views
Avatar
Bruce Rochester

I've added a new partner type:

type = fields.Selection(selection_add=[('service', 'Service address')])  

I would like to hide this new partner type from the "Contacts & Addresses" one2Many on the res.partner form, using the following XML:

<xpath expr="//field[@name='child_ids']" position="attributes"><attribute name="domain">[('active', '=', True), ('type', '!=', 'service')]</attribute>            </xpath> 

In debug mode, I can see the domain has been updated on the form, but it is still showing all records here. I've even tried the domain [('id', '=', False)], but all records are still showing.

Anyone know what's going on here?

As Zbik helpfully pointed out below, domain can not be set through XML for a one2Many. How can I create two different one2Many fields in different views that show a different subset of the list items?

0
Avatar
Discard
Sehrish

Use Context: https://goo.gl/XXg5D3

Avatar
Bruce Rochester
Author Best Answer

Turns out this is apparently a lot more complicated than it should be. My workaround was to create 2 computed one2many fields. One for service addresses and one for other addresses.

    service_addresses = fields.One2many(
        'res.partner', string="Service Addresses",
        compute='_compute_address_types'
        )
    other_addresses = fields.One2many(
        'res.partner', string="Contacts & Addresses",
        compute='_compute_address_types'
        )

    @api.depends('child_ids')
    def _compute_address_types(self):
        for res in self:
            service_addresses = res.child_ids.filtered(lambda x: x.type == 'service')
            other_addresses = res.child_ids.filtered(lambda x: x.type != 'service')
            res.service_addresses = [(6, 0, [x.id for x in service_addresses])]
            res.other_addresses = [(6, 0, [x.id for x in other_addresses])] 

I replaced the one2many on the partner form with other_addresses, and service_addresses on a separate tab.

Of course, these computed fields then didn't have an add button, so I also added child_ids one2many below each of them with a blank kanban template, so the kanban list doesn't show up, only the "ADD" button.

Clunky, but seems to work.


0
Avatar
Discard
Avatar
Zbik
Best Answer

One2many domain not works in XML. Try it in python code.

1
Avatar
Discard
Bruce Rochester
Author

Thanks, the service addresses no longer show up, but this now seems entirely pointless - service addresses will never show up in ANY view if I can't change the domain per view.

Bruce Rochester
Author

Is it not possible to create 1 one2Many field, then have 2 different views that show a different subset of the list?

Zbik

You can have many views depending on the permissions or the action being called.

Bruce Rochester
Author

I understand I can have many views, but the domain will always be the same because I can't set it in the XML. I have set the domain in Python:

child_ids = fields.One2many('res.partner', 'parent_id', string='Contacts', domain=[('type', '!=', 'service')])

Now, any time I display that One2many field it will not show the service addresses.

I would like 1 view to have the domain: [('type', '!=', 'service')] and the other one with domain [('type', '=', 'service')]...

Zbik

Maybe build an additional computed domain control field?

Bruce Rochester
Author

Thanks, but I'm not quite sure I understand. I've tried assigning the domain using a lambda function, which seems to work but I'm not sure how to determine which view is triggering the function.

child_ids = fields.One2many('res.partner', 'parent_id', string='Contacts', domain=lambda x: x._get_contacts_domain())

def _get_contacts_domain(self):

print("debug")

print(self)

return [('type', '!=', 'service')]

I put a breakpoint on the print lines, and there is nothing I can see in the context that tells me which view is requesting the domain. Is there some other variable I should be looking at?

Zbik

You pass a context from views ==> view set a context and domain get it ==> self._context.get('xxx')

or nwe field is default set from context and domain use it

Bruce Rochester
Author

None of the context information is being passed to my _get_contacts_domain function

Zbik

Then modify xml and build this context yourself!

Bruce Rochester
Author

But the context already exists in the XML! My code:

<field name="child_ids" mode="kanban" context="{'default_parent_id': active_id, 'default_street': street, 'default_street2': street2, 'default_city': city, 'default_state_id': state_id, 'default_zip': zip, 'default_country_id': country_id, 'default_supplier': supplier, 'default_customer': customer, 'default_lang': lang, 'default_user_id': user_id}">

When I load the page, it triggers my function _get_contacts_domain() several times, and the context is never being passed.

Here is all I get when I print self._context from that function:

{'lang': 'en_CA', 'tz': 'Canada/Eastern', 'uid': 2, 'base_model_name': 'res.partner', 'view_is_editable': None}

Zbik

Context defined in XML line child_ids is used when subviews (defined in this line) are prepared to view.

When your view is builded, and when your _get_contacts_domain is called, context is defined and get from parent action.

In your case, probably this is action == 'contacts.action_contacts'

Bruce Rochester
Author

Helpful, but also raises another issue. If we can't use field-level context, we can't differentiate between the 2 lists if they are on the same view. This should help if I want to include only 1 list per view, but won't allow me to have different domains on the two lists. I've ended up using a work-around since this functionality doesn't seem to exist in Odoo.

Zbik

I really don't understand why you add a new type = "service" if you already have "delivery" and "other"

Bruce Rochester
Author

Because we have customers with thousands of service addresses that we don't want mixed in with the few other addresses they have.

Bruce Rochester
Author

And if we use "delivery" or "other" address we still have to deal with the exact same problem. Using a new type of address is NOT the root of the issue.

Zbik

Ok, but the new type will probably create problems for you in many other cases.

Bruce Rochester
Author

OK thanks for that input. I will make sure to investigate the possible side-effects of using a custom partner type by analyzing the source code. Cheers.

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
Odoo Mail Sending Limit Solved
odoo12
Avatar
Avatar
Avatar
2
Dec 23
16360
(Document type: Invoice, Operation: write) - (Records: [], User: 2)
odoo12
Avatar
0
Oct 23
33
Error while importing data in Odoo12: An unknown issue occurred during import (possibly lost connection, data limit exceeded or memory limits exceeded)
odoo12
Avatar
Avatar
Avatar
Avatar
3
Oct 23
790
Remove duplicate record when importing data from excel to Odoo
odoo12
Avatar
Avatar
1
Oct 23
569
Multiple group on field Odoo12
odoo12
Avatar
Avatar
1
Aug 23
3341
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