This question has been flagged
6 Replies
10327 Views

I am trying to add a new column of product data (product.template model) to purchase.order.form view.

The column should contain a custom attribute like x_bla.

When using just the "usual way" by adding: <field name="x_bla"/> I am getting an error:


Field(s) `arch` failed against a constraint: Ungültige Ansichtendefinition

Error details:

The field `x_bla` doesn't exist.

Fehler Kontext:

Ansicht `purchase.order.form`

[view_id: 789, xml_id: purchase.purchase_order_form, model: purchase.order, parent_id: k. A.]


So I need to add a field into purchase.order model and connect it to my custom attribute... but how does that work?


Any hints in the right direction?

Avatar
Discard
Author

@Frederico. The attribute is from product.template model. I assume you added the attribute to purchase.order model? So my next question would be how to link an attribute from purchase.order to product.template...?

Then you create a field just using the Odoo GUI. I'm afraid, but you need a little bit of python to inherit the fields list, using fields.related on product.py and/ or on purchase.py. What kind of field is your "x_bla"? Is already populated or can be substitute with a field created on model .py? I'm not the best expert around here but tomorrow I'll test the code on my dev-server.

Author

Unfortunately yes. I looked into the purchase.py and found the product_id field with related info to product.product and tried to do the same for my custom attribute but it didn't work. I also think that a custom module for adding an attribute and relations to the models there would be more promising... I'm looking forward to any help with this. Would be my first module to write "by myself"... so any help appreciated...

Thomas, if the field is already populated you can easily resolve with a simple export and a batch input. I'm not a big fan of creation of fields inside odoo, it's better to create a new module for inherit the models and views you need. If you don't know, you could create an empty module ready to be filled with your information with the command [code] python odoo.py scaffold addons [/code]. In that way you will have a correct layout. I'll have a look on server and then I'll update my post.

Author

thank you akhil, I just tried to do so. check out here: https://github.com/thomasklosinsky/product_kunstler But still having errors. I think the related field in .py is not correctly set and the view in .xml also... Help appreciated!

Best Answer

Hi Thomas,


You can inherit product.template model and the field "x_bla". And you want that field in purchase order means, it should appear as a column in the product lines, right?

So you need to add one more field in "purchase.order.line" model.And you have to override the onchange_product_id function in that model. That means when you are adding a product in purchase order, along with product description, qty, unit price etc, our "x_bla" field value will also get filled.

In view file, you need to inherit the tree view of purchase.order.line and add your "x_bla" field also.


If you are working with odoo v8 new api, one more easier way is to define a related field in "purchase.order.line" model.

Eg:  field_name = field.Char(related="product_id.x_bla", string="New field") #if your x_bla is a char field.

And then add this field to the tree view of purchase.order.line, by inheriting and using xpath.

But prefer the first method ;)

If you have any doubts in code or something related to this, I can help you to understand the things better.

Avatar
Discard

I found the new API a little complicated to manage when you are working on "stock" py, better create a new module just for inherit models. On point_of_sale.py for example using the new api format always give me errors.

Best Answer

Please verify if the field "x_bla" was correctly created. I tested it right now on purchase.order.form (purchase.order) and is working fine.

For my test I've created a char(255) field named "x_bla" on purchase.order model always searchable and I've successfully added it under <field name="date_order"/> on purchase.order.form view .

Avatar
Discard