Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
4652 Lượt xem

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)
Ảnh đại diện
Huỷ bỏ

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

Câu trả lời hay nhất

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 

Ảnh đại diện
Huỷ bỏ
Tác giả

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

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 9 21
5354
1
thg 7 19
2844
7
thg 11 15
14022
4
thg 10 22
4868
2
thg 9 21
2926