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

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?



Avatar
Discard
Author

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?

Best Answer

To limit the selection of the models to the model loaded to the document_model_id field, you can use the following code:

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=lambda self: self._reference_models())

    def _reference_models(self):
        if self.document_model_id:
models = self.env['ir.model'].search([('model', '=', self.document_model_id.model)])
 
      else:
models = self.env['ir.model'].search([('state', '!=', 'manual')])
return [(model.model, http://model.name" target="_blank" data-saferedirecturl="model.name)" rel="ugc">https://www.google.com/url?q=http://model.name&source=gmail&ust=1688454077935000&usg=AOvVaw1mvVA_XY05kd8F2DdgS_RC">model.name) for model in models]




Hope it helps

Avatar
Discard
Related Posts Replies Views Activity
1
Jul 22
2110
1
May 25
66
0
May 25
203
1
May 25
406
1
May 25
466