Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

python - Odoo 12 how to display model records in one field, depending on the model chosen in other field

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

enter image description here

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

enter image description here

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...