Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
3461 Weergaven

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
Annuleer
Auteur

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?

Beste antwoord

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
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
1
jul. 22
2149
1
mei 25
94
0
mei 25
255
1
mei 25
461
1
mei 25
548