Odoo 12如何在一个字段中显示模型记录,具体取决于在其他字段中选择的模型

2024-05-16 17:58:02 发布

您现在位置:Python中文网/ 问答频道 /正文

经过几个小时和不同的方法,我无法得到我需要的东西

我正在开发一个模块来配置产品中不同类型的属性。类别,一旦为产品分配了类别id,我会将该类别的所有属性复制到此产品,以便用户分配每个人的值

非常简单,模型product.category.properties

class ProductCategoryProperties(models.Model):
    _name = 'product.category.properties'
    _order = "category_id, name"

    #CONFIG. OPTIONS
    VALUE_TYPES = [('char', 'Texto corto'), ('text', 'Texto largo'), ('num', 'Número'), ('oper', 'Operación'), ('list', 'Lista')]
    OPERATION_TYPES = [('sum','Suma'),('sub','Resta'),('mul','Multiplicación'),('div','División')]

    #FIELDS
    category_id = fields.Many2one('product.category', string='Categoría', required=True, ondelete='cascade', index=True, copy=False)
    name = fields.Char(string='Nombre', translate=True, required=True)
    description = fields.Text(string='Descripción', translate=True)

    value_type = fields.Selection(selection=VALUE_TYPES, default='char', string='Tipo valor', size=64, help='Tipo de valor de la propiedad', required=True)
    value_min = fields.Float(string="Mínimo", help="Valor mínimo permitido")
    value_max = fields.Float(string="Máximo", help="Valor máximo permitido")
    operation_type = fields.Selection(selection=OPERATION_TYPES, default='sum', string='Tipo operación', size=64, help='Tipo de operación de la propiedad')
    operation_field1 = fields.Many2one('product.category.properties', string='Campo 1', ondelete='cascade')
    operation_field2 = fields.Many2one('product.category.properties', string='Campo 2', ondelete='cascade')
    model_id = fields.Many2one('ir.model', string="Lista", ondelete='cascade')

enter image description here

模型product.properties(填充product.categu_id时从product.category.properties复制的属性)

class ProductProperties(models.Model):
_name = 'product.properties'
_order = "product_id, product_category_property_id"

#FIELDS
product_id = fields.Many2one('product.template', string='Producto', required=True, ondelete='cascade', index=True, copy=False)
product_category_property_id = fields.Many2one('product.category.properties', string='Propiedad', required=True, ondelete='cascade', index=True, copy=False)
value = fields.Char(string='Valor')
value_record = fields.Selection(string='Valor registro', selection='get_model_values', size=64)
value_wizard = fields.Integer(string='Valor wizard')

hide_record = fields.Boolean(string='Ocultar', compute='_hide_record', store=True)

#FUNCTIONS
def get_model_values(self):
    #vals = [('1','Uno'), ('2','Dos')]
    vals3 = []
    
    res_partner = self.env['res.partner'].search([])
    for i in res_partner: vals3.append([i.id, i.name])
    
    uom_uom = self.env['uom.uom'].search([])
    for j in uom_uom: vals3.append([j.id, j.name])

    return vals3

enter image description here

正如您所看到的,这很容易,还有其他函数可以验证值是否在最小值和最大值之间,以及计算操作,例如Superficie(50)=Ancho(5)*Largo(10)

当我需要“Lista”的字段“Valor”来显示模型记录时,问题就出现了,具体取决于所选的模型(“Lista”.model_id”),在本例中是Contacto(res.partner),但可以是任何人

我试过: -不同的字段类型(值、值记录、值向导) -尝试了一些函数,如“def get_model_values”,但需要对模型进行硬编码,并且实际上无法显示不同的模型记录,例如一个记录的res.partner(Lista)和其他记录的其他模型(Lista2) -听说过显示向导以每次显示不同型号的记录并返回所选记录的id或其他字段,但不知道如何显示 -听说过使用javascript/jquery直接在前端替换值,但不知道如何替换

我也看到mail.message有一个res_id整数字段,用于存储不同记录模型的id。模型可以通过这种方式解决,但问题是我需要显示记录列表供用户选择

任何能帮忙的人都将不胜感激。 谢谢


Tags: name模型idtruefieldsstringvalue记录