Skip to Content
Menu
This question has been flagged

Hello,

Generally when a many2one field is declared and a model is referred to, this dropdown list populates the entries from the field name but i want to populate the drop down list with a custom field say companyName. How can i achieve this? Can anyone refer me to some example..

Thanks

Avatar
Discard
Best Answer

Hello,


Example of dropdown list from another model.


.py file : 


class ResPartner(models.Model):

    _inherit = 'res.partner'

software = fields.Many2one('partner.software', string='Software')

#this is the dropdown list


class PartnerSoft(models.Model):

    _name = 'partner.software'

    _description = 'Software'


    software = fields.Char('Soft')

    _rec_name = 'software'

#this is where I save my custom values

xml file for the dropdown list : 


        <record id="res_partner_form_view_extension" model="ir.ui.view">

            <field name="name">res.partner.form.view.extension</field>

            <field name="inherit_id" ref="base.view_partner_form"/>

            <field name="model">res.partner</field>

            <field name="priority" eval="2"/>

            <field name="arch" type="xml">

                <xpath expr="//field[@name='lang']" position="after">

                    <field name="software"/>

                </xpath>

            </field>

        </record>



xml file for the second model where you store the dropdown list values: 


<odoo>

    <data>


        <record id="res_partner_soft_form_view" model="ir.ui.view">

            <field name="name">res.partner.soft.form.view</field>

            <field name="model">partner.software</field>

                <field name="arch" type="xml">

                    <form>

                        <sheet>

                            <div>

                                <label for="soft" string="Software" class="oe_edit_only"/>

                                <h1><field name="software"/></h1>

                            </div>

                        </sheet>

                    </form>

                </field>

        </record>


        <record id="res_partner_soft_tree_view" model="ir.ui.view">

            <field name="name">res.partner.soft.tree.view</field>

            <field name="model">partner.software</field>

            <field name="arch" type="xml">

                <tree string="software">

                    <field name="software"/>

                </tree>

            </field>

        </record>


        <record id="res_partner_soft_search_view" model="ir.ui.view">

            <field name="name">res.partner.soft.search.view</field>

            <field name="model">partner.software</field>

                <field name="arch" type="xml">

                    <search string="Software">

                        <field name="software"/>

                    </search>

                </field>

        </record>


        <record model="ir.actions.act_window" id="partner_soft_action">

            <field name="name">Software</field>

            <field name="res_model">partner.soft</field>

            <field name="view_mode">tree,form</field>

            <field name="view_id" ref="res_partner_soft_tree_view"/>

            <field name="view_type">form</field>

            <field name="search_view_id" ref="res_partner_soft_search_view"/>

       </record>



        <menuitem id="partner_soft_menu_item"

          name="Software"

          action="partner_soft_action"

          parent="contacts.menu_contacts"/>


    </data>

</odoo>

Avatar
Discard
Best Answer

Hello

you can achieve this using the

1. override the name_get() or

2. using the _rec_name = "<field-name>".  for eg. _rec_name = 'company_name'


Avatar
Discard
Author

Below is how i tried and it didn't work... Did i miss something?

class AccountAnalyticAccount(models.Model):

_inherit = 'account.analytic.account'

_rec_name = 'inHeader'

inHeader = fields.Char(string='Company Name', required=True)

class headerSelection(models.Model):

_inherit = 'account.invoice'

headerSelect = fields.Many2one('account.analytic.account', string="Company Name", required=True)

Related Posts Replies Views Activity
0
Mar 23
774
4
Mar 19
6670
2
Nov 16
5854
0
May 20
2601
5
Aug 19
8021