跳至内容
菜单
此问题已终结
1 回复
4720 查看

I have added a field in product.template named 'model' in my custom module and added this field in my view. But the problem is it is showing product list in the drop down when creating record.

Here is my 

models.py

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

model = fields.Char(string="Product Model", required=False)

class GearManufacturing(models.Model):
_name = 'gear.manufacturing'
product_id = fields.Many2one("product.product", 'Product',required=True)
model = fields.Many2one('product.template', 'Model',required=True)
product_to_dispatch = fields.Integer('Products to Dispatch',required=True)
dispatch_date = fields.Date('Dispatch Date', copy=False, default=fields.Date.today(), index=True, required=True)
views.xml
<record id="gear_manufacturing_tree" model="ir.ui.view">
<field name="name">gear.manufacturing.tree</field>
<field name="model">gear.manufacturing</field>
<field name="arch" type="xml">
<tree>
<field name="product_id"/>
<field name="product_to_dispatch"/>
<field name="model"/>
<field name="dispatch_date"/>
</tree>
</field>
</record>

The field model is showing me Products in dropdown however, it should display nothing since there are no records of it. Where am I going wrong? 


Update:

When I upgrade my module, I am getting this error too:


019-11-24 05:34:14,321 27436 ERROR db_test odoo.sql_db: bad query: b"\n                INSERT INTO ir_model_data (module, name, model, res_id, noupdate, date_init, date_update)\n                VALUES ('gear_manufacturing', 'access_gear_manufacturing_gear_manufacturing', 'ir.model.access', 616, false, now() at time zone 'UTC', now() at time zone 'UTC'), ('gear_manufacturing', 'access_gear_manufacturing_gear_manufacturing', 'ir.model.access', 617, false, now() at time zone 'UTC', now() at time zone 'UTC')\n                ON CONFLICT (module, name)\n                DO UPDATE SET date_update=(now() at time zone 'UTC') \n            "
ERROR: ON CONFLICT DO UPDATE command cannot affect row a second time
HINT: Ensure that no rows proposed for insertion within the same command have duplicate constrained values.

2019-11-24 05:34:14,321 27436 ERROR db_test odoo.addons.base.models.ir_model: Failed to insert ir_model_data
('gear_manufacturing', 'access_gear_manufacturing_gear_manufacturing', 'ir.model.access', 616, False)
('gear_manufacturing', 'access_gear_manufacturing_gear_manufacturing', 'ir.model.access', 617, False)
形象
丢弃

it seems there is an issue with metadata (ir_model_data). I think it will solve when you create a new database (if feasible)

最佳答案

above example works as it should there is no issue with code.

I think you need new model for product model instead of inheriting it from product.template 
add product model as many2one with gear.manufacturing like

class ProductModel(models.Model):
    _name = 'product.model'
    name = fields.Char('Product Model')

class GearManufacturing(models.Model):
    ......
   model_id = fields.Many2one('product.model', 'Model',required=True)

if you need a product model on product.tempalate you can also add many2one relation there 

形象
丢弃
编写者

Ravi, Updated my question. I am getting error while upgrading my module. Maybe this pertain with the question I have posted

相关帖文 回复 查看 活动
2
9月 21
5413
1
7月 19
2875
7
11月 15
14134
4
10月 22
4938
2
9月 21
2981