Hey,
I am implementing simple workflow engine and i have a concept of a workflow and a workflow instance. Workflow can operate on a selected model, therefore the workflow instances of that workflow will contain the reference to an instance of that model.
I am trying to use the reference field to select any model in the database with some constraints.
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 the place of the question marks to limit the selection of the models to the model loaded to the document_model_id field?
I have found the following snippet but it seems not to have the context of the current record and simply does not work:
@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.')]
Also, when used as a selection attribute, the function seems to be called only once, so I see no way of updating this.
How would you approach this?
I have found Many2oneReference type of field:
https://www.odoo.com/documentation/15.0/developer/reference/backend/orm.html#pseudo-relational-fields
This seems to suit my needs, however I can't find an appropriate widget allowing for the selection.
Has anyone implemented such a widget?