1

I have added a simple One2many field in product.template model and I also added one2many widget in product.template form view. I can see the widget, but when I try to add a new record I get this error: ValueError: Invalid field 'product_variant_count' on model '_unknown'.

product.template.substitute model

class ProductTemplateSubstitute(models.Model):
    _name = "product.template.substitute"
    _description = "Product Substitute"
    _rec_name = "manufacturer_part_number"

    manufacturer_part_number = fields.Char(string="Manufacturer Part Number", required=True)
    product_id = fields.Many2one(string="Product", required=True)

    _sql_constraints = [
        (
            "unique_manufacturer_part_number",
            "unique (manufacturer_part_number)",
            "Manufacturer part number already exists!",
        )
    ]

product.template model

class ProductTemplateInherited(models.Model):
    _inherit = "product.template"

    product_substitute_ids = fields.One2many(
        string="Product Substitutes", comodel_name="product.template.substitute", inverse_name="product_id"
    )

product.template view

<odoo>
    <record id="product_template_form_view" model="ir.ui.view">
        <field name="name">product.template.form.view.inherit.vendor.eshop.connector</field>
        <field name="model">product.template</field>
        <field name="inherit_id" ref="product.product_template_form_view"/>
        <field name="arch" type="xml">
            <group name="bill" position="after">
                <group name="product_substitutes" string="Substitutes">
                    <field name="product_substitute_ids" widget="one2many" nolabel="1">
                    <tree editable="bottom">
                        <field name="manufacturer_part_number"/>
                    </tree>
                </field>
                </group>
            </group>
        </field>
    </record>
</odoo>

And when I click on Add a line button in one2many widget I get the following error enter image description here

enter image description here

The weird thing is, that I am not using product_variant_count field mentioned in the error message anywhere in my code.

I would be really thankful for any help because I am really desperate.
Odoo-14, Python 3.9

SOLUTION: It was a dumb mistake. I forgot to add comodel_name in my Many2one field

# Wrong
product_id = fields.Many2one(string="Product", required=True)
# Correct
product_id = fields.Many2one(string="Product", comodel_name="product.template", required=True)
xixo222
  • 157
  • 2
  • 9
  • 1
    I’m voting to close this question because OP has already found the solution to the question. There is no need for this question to receive anymore answers. – holydragon Feb 02 '23 at 06:32

1 Answers1

1

I am posting the solution in the comments as well.

It was a dumb mistake. I forgot to add comodel_name in my Many2one field:

# Wrong
product_id = fields.Many2one(string="Product", required=True)
# Correct
product_id = fields.Many2one(string="Product", comodel_name="product.template", required=True)
Michael M.
  • 10,486
  • 9
  • 18
  • 34
xixo222
  • 157
  • 2
  • 9