Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
2 Відповіді
1383 Переглядів

Hi everyone,


Is it possible to display a list of one2many field records based on another model's field instead of the model name field? For example, in the related model, there is a field named "code." I would like to list all records in the one2many field but display the "code" field instead of the names of the records.

Аватар
Відмінити
Найкраща відповідь

Hi,


Yes, it's possible to display a One2many field using a custom field (like code) instead of the default name. You can achieve this by using the many2many_tags widget with the display_name option.


Python Code


class Model1(models.Model):

    _name = 'your.module.model_1'

    _description = 'Model 1'


    name = fields.Char(string='Name')

    model_2_ids = fields.One2many('your.module.model_2', 'model_1_id', string='Model 2 Records')



class Model2(models.Model):

    _name = 'your.module.model_2'

    _description = 'Model 2'


    code = fields.Char(string='Code')

    model_1_id = fields.Many2one('your.module.model_1', string='Model 1')


XML View


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

    <field name="name">model1.form</field>

    <field name="model">your.module.model_1</field>

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

        <form string="Model 1">

            <sheet>

                <field name="model_2_ids" widget="many2many_tags" options="{'display_name': 'code'}"/>

            </sheet>

        </form>

    </field>

</record>


Hope this helps

Аватар
Відмінити
Найкраща відповідь

you can simply add:

class RelatedModel(models.Model):

    _rec_name = 'code'


​code = fields.Text('Code')

to your one2many related model

Аватар
Відмінити
Related Posts Відповіді Переглядів Дія
1
січ. 25
693
0
лист. 22
132
2
вер. 22
8427
2
квіт. 22
3267
0
лип. 21
6112