Help

0

using name_get how to shows two different display values?

Avatar
Prakash

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

1 Comment
Avatar
Discard
3 Answers
2
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):

    <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))

Regards,

Yenthe

3 Comments
Avatar
Discard
Avatar
Sugeesh Ps
-

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 ?

Avatar
Nicolas de Moreau
-

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... ;-)

Avatar
BT-amascherpa
-

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

7
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}'/>
1 Comment
Avatar
Discard
Avatar
omprakash
-

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

1
Avatar
Abegail Sanchez
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