Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
2505 Lượt xem

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?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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 

Ảnh đại diện
Huỷ bỏ

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)])]

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 7 23
3862
1
thg 8 25
427
1
thg 8 25
408
4
thg 7 25
1719
1
thg 7 25
981