Skip to Content
Menu
This question has been flagged
1 Reply
1608 Views

Hi there,


I am trying to use a reference field to allow any model selection. I have the following models


class eaxpl_workflow(models.Model):
_name = 'eaxpl.workflow'

    document_model_id = fields.Many2one('ir.model')

class eaxpl_workflow_instance(models.Model):
_name = 'eaxpl.workflow_instance'

workflow_id = fields.Many2one('eaxpl.workflow', string="Workflow", required=True)
document_model_id = fields.Many2one('ir.model', related='workflow_id.document_model_id')

document_reference = fields.Reference(
selection=??????
)

The question is what to put in selection attribute of the reference field to limit the model selection to the model which is populated in the document_model_id field?


I have found the following snippet, but it has no context of the current model.


    @api.model
def _reference_models(self):
models = self.env['ir.model'].sudo().search(
[('state', '!=', 'manual')])
return [(model.model, model.name)
for model in models
if not model.model.startswith('ir.')]

Is it even possible? How would you approach such a problem?

Avatar
Discard
Best Answer

Hello Artur Roszczyk,

You need to add the model list as per your requirement in the selection attribute in the document_reference field.

Please find code in Comment. 

Also, you can refer to below documentation link for better understanding.
https://www.cybrosys.com/blog/reference-fields-in-odoo

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari 

Avatar
Discard

document_reference = fields.Reference(selection=’_selection_target_model’)

@api.model
def _selection_target_model(self):
return [(model.model, model.name) for model in self.env['ir.model'].sudo().search([('name', '=', self._name)])]

Related Posts Replies Views Activity
1
Jul 23
2726
1
Dec 24
66
0
Dec 24
37
1
Dec 24
46
0
Dec 24
55