How to get the list of models of a Reference field

A Reference field basically consists of two fields:

  1. Selection field, so the user can select a model
  2. Many2One field, so the user can select a record from the selected model

The question is now: How do I programmatically get the list of models which is needed for the Selection field? What RPC command can I use for that?

It is defined in the field definition by the attribute selection.

When the selection refers to a method, the method is available for RPC.

Hmm, when I get the field definition by
model.calendar.item.fields_get(["origin"]) I get an object back with all the parameters. Also I see

{
....
selection: "get_origin"
sort: true
​​sortable: true
....
​}

So I did a model.calendar.item.get_origin and surprisingly I got an array back with the models! I dug a bit deeper into my code and indeed:

origin = fields.Reference('Origin', selection='get_origin')

@classmethod
def get_origin(cls):
    Model = Pool().get('ir.model')
    models = cls._get_origin()
    models = Model.search([
            ('model', 'in', models),
            ])
    return [('', '')] + [(m.model, m.name) for m in models]


Tryton is such a nice piece of software :smiley:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.