Skip to Content
Menu
This question has been flagged
14 Replies
7542 Views

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?

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


Avatar
Discard
Best Answer

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

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

Author

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

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

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')]...

Maybe build an additional computed domain control field?

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?

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

Author

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

Then modify xml and build this context yourself!

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}

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'

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.

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

Author

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

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.

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

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.

Related Posts Replies Views Activity
2
Dec 23
12005
0
Oct 23
33
3
Oct 23
787
1
Oct 23
569
1
Aug 23
980