After several hours and differente approaches, can't reach to get what I need.
I'm developing a module to configure different kind of properties in a product.category, once a product is assigned a categ_id, I copy all the properties of this category to this product, for the user to assign the value of everyone.
Is pretty simple, model 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')
Model product.properties (properties copied from product.category.properties when product.categ_id is filled.
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
As you can see that's easy, there are other functions to validate that values are between Min and Max values, and to calculate operations, for example Superficie (50) = Ancho (5) * Largo (10).
The problem comes when I need field "Valor" of "Lista" to display model records depending on the model chosen ("Lista".model_id), in this case Contacto (res.partner) but could be anyone.
I have tried with:
- different field types (value, value_record, value_wizard)
- tried some function like "def get_model_values", but needs to have the model hardcoded and doesn't really work to show different model records, for example res.partner for one record (Lista) and other model for other record (Lista2)
- heard about displaying a wizard to show records of a different model every time and return the id or other field of the selected record, but don't know how
- heard about using javascript/jquery to replace values directly on front, but don't know how
I have seen too that mail.message has a res_id integer field, that stores id of different record models. Model could be solved this way, but the problem is that I need to display record lists for the user to choose.
Anyone that could help would be appreciated.
Thanks!
question from:
https://stackoverflow.com/questions/65836363/odoo-12-how-to-display-model-records-in-one-field-depending-on-the-model-chosen