Skip to Content
Menu
This question has been flagged
4 Replies
53507 Views

I have two fields related with res.partner.addreess

_name = "contact_details"
_columns = {
        'contact_id': fields.many2one('res.partner.address', 'Contact Name'),
        'contact_address_id': fields.many2one('res.partner.address', 'Contact Address'),
 }

In the contact_details Form:-

Currently display

Contact Name --> Contact Name with address

Contact Address --> Contact Name with address

Actual Requirement

Contact Name --> Contact Name only

Contact Address --> Contact Name with address

Using name_get Is it possible using name_get to show contact Name and Contact Address in Field 1 and field 2 ???

Thanks

Avatar
Discard
Best Answer

Hi all,

While the answer from Fabien is technically correct I've noticed something interesting. If you set the context on a field the name_get() function is only changed when you're in edit mode and atleast touched the field ones. If you're not in editable mode or didn't touch the field atleast once the context is not passed along to the name_get() function and thus it shows the default rec_name value. If you pass the context on the menuitem itself it will alter the name_get values as the context is set before the name_get() goes off. 

An example (tested in V12 & V13):

    <record id="your_action" model="ir.actions.act_window">
        <field name="name">Your action</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">res.partner</field>
        <field name="context">{'custom_search': True}</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,form</field>
        <field name="search_view_id" ref="your_view_search"/>
        <field name="help" type="html">
            <p class="oe_view_nocontent_create">
                Click to add a new record.
            </p>
        </field>
    </record>

def name_get(self): result = [] for record in self:
if self.env.context.get('custom_search', False):
# Only goes off when the custom_search is in the context values. result.append((record.id, "{} {}".format(record.name, record.address)))
else:
result.append((record.id, record.name))
return result

Regards,

Yenthe

Avatar
Discard

I have used name_get() function and i could change the default value, But i need this change in only one many2one all the other many2one with the same relation should show the rec_name value. How can i make this possible ?

Hi Yenthe, thanks for your answer. Maybe you could consider adding "return result" in the name_get. I know it's obvious but I copy-pasted it and it took me some time finding the error... ;-)

Hi Yenthe, the Fabien solution works fines if you add: options={'always_reload': True} on the field, as stated on his answer. This way it works fine on edit and save mode

Best Answer

Yes, you must put a context on one of the two fields (in the view or in the field) and develop a name_get on thre res.partner object that returns different values according to the context.

OpenERP has already such an implementation for partners. This should work in your view, for v7:

<field name="contact_id"/>
<field name="contact_address_id" context="{'show_address': 1}" options='{"always_reload": True}'/>
Avatar
Discard

Hi Fabien Pinckaers , I have doubt regards passing context value in XML file . Can you please explain with example .

now in odoo 14 it is partner_category_display

and it is not working when you set it to short

Best Answer

Hi Fabien,

this helped me, thanks

this was he meant

def name_get(self,cr,uid,ids,context=None):
        if context is None:
            context ={}
        res=[]
        record_name=self.browse(cr,uid,ids,context)
        for object in record_name:

                if object.name
                   if context.get('show_address',False):
                          //name for contact_address_id field

                         res.append((object.id,object.name+object.address))
                  else:
                       //name for contact_id field                     

                      res.append((object.id,object.name))
        return res

Avatar
Discard
Best Answer

Hi everyone.
We're facing this issue as well, exactly the same as described by Yenthe, but although I see some comments saying it works, we've not managed to make it work, even playing with the 'always_reload' option. (Odoo 12!)

name_get function does not trigger unless you're in edit mode (talking about m2m and o2m in my case)

Example: wizard.compose.mail has a res.partner many2many field shown as tags to display the recipients of the email. If we search partners by emails in the m2m, it works in edit mode, but after we've selected any record it keeps showing as "Companyname, name". 

Context is properly set in an inherited name_get func, that works in other places, but not here. Always_reload option is also to set to True.

Got any idea?  BT-amascherpa replied saying it worked, but on which version? This used to work for me as well in v8 even without always_reload, but we've not managed to make it work in v12.

Thanks.



Avatar
Discard