将属性与名称产品分开Odoo 14

问题描述

对不起我的英语。我需要帮助解决我遇到的问题。我定义了一个函数来为产品带来价值属性,但是当我运行它时,结果是

ValueError: Expected singleton: product.template.attribute.value(9,25)

有人指导我解决吗?我不知道怎么继续

class MRPSalesProduc(models.Model):
    _inherit = 'mrp.production'

    product_short_name = fields.Char('Productos')

    @api.model
    def create(self,vals):
        product_short_name = self.env['sale.order'].search([('name','=',vals[
            'origin'])]).order_line.product_id.product_template_attribute_value.attribute_id
        vals['product_short_name'] = product_short_nam
        rec = super(MRPSalesProduc,self).create(vals)
        return rec

解决方法

您已尝试搜索销售订单并访问所有销售订单行。

如果您从销售订单行中选择第一行,那么它将按您的预期工作。例如,

sale_order = self.env['sale.order'].search([('name','=',vals['origin'])])
product_short_name = sale_order and sale_order.order_line[0].product_id.product_template_attribute_value.attribute_id

if product_short_name:
    vals['product_short_name'] = product_short_nam

您可以参考我的博客 https://odedrabhavesh.blogspot.com/2017/02/valueerror-expected-singleton-in-odoo.html 以了解有关“ValueError: Expected singleton”的更多信息

,

您可以使用 related many2many 字段来获取产品属性

示例:

class MRPSalesProduct(models.Model):
    _inherit = 'mrp.production'

    product_template_attribute_value_ids = fields.Many2many(related='product_id.product_template_attribute_value_ids')  

然后使用 man2many_tags 小部件将产品属性显示为标签:

<field name="product_template_attribute_value_ids" widget="many2many_tags"/>  

示例(产品名称):

class MRPProductsName(models.Model):
    _inherit = 'mrp.production'

    products_name = fields.Char(related="product_id.product_tmpl_id.name",string='Productos')

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...