This question has been flagged
2 Replies
12751 Views

Hi, I have created a related field on the product.template form in hope to duplicate the field and have it placed somewhere else.

In the _columns{} section of a class, I have added :

'default_code_duplicate' : fields.related('product_variant_ids', 'default_code', type='char', string='SKU:'),

And in the view I have added:           

           <field name="arch" type="xml">
                <div name="options" position="inside">
                    <div>
                        <label for="default_code_duplicate"/>
                        <field name="default_code_duplicate"/>
                    </div>
                </div>
            </field>

The field shows up fine and i thought it was working correctly until I created a new record and inputted data to this field. The data dissapears on 'Save'. When I click edit, then enter again in the same field, and click save - the data stays.

Could this be something to do with the fact it is a temporary record when you click create, and not written to the database yet?

Is this the correct way to go about this? I don't actually need to duplicate the field, but simply move it - but i didn't know how to do that - so I went about doing it this way.

Thanks

 

Avatar
Discard
Best Answer

Hello Peter Alabaster,

No, using a related field is not the correct way to "move" a field on a view.

First, related field are not meant to be written when creating a new record (as you saw it on your tests, the create() method remove the related fields from the written fields).

Then, you can move a field by directly overriding the view (you have to create a new view on xml file, and add the field inherit_id to reference the product view).

Moving a field can be dangerous, as other modules can override the same view. One of the correct way to do this could be (IMHO), to set the priority field of your view as higher as possible (so that your view will be the last one to override the original view).

To sum'up, you could create a new view to remove the original field and relocate it where you want to move it, for example: 

        <record id="view_product_peter_alabaster_inherit" model="ir.ui.view">
            <field name="name">product.product.peter.alabaster.inherit</field>
            <field name="model">product.product</field>
            <field name="priority">99</field>
            <field name="inherit_id" ref="product.product_view_you_want_inherit"/>
            <field name="arch" type="xml">
                <-- remove the orinial location of default_code and its label -->
                <field name="default_code" position="replace"/>
                <label for="default_code" position="replace"/>
                <!-- relocate the field under the options field -->
                <field name="options" position="inside">
                    <div>
                        <label for="default_code"/>
                        <field name="default_code"/>
                    </div>
                </field>
        </record>

 

Hope it helps you,

Regards

Avatar
Discard
Author Best Answer

Sorry I can't reply to your comment. Thankyou for the answer! It is very helpful. I think i must have tried this before though.

The product template form is all messed up - default_code is in a different form inherit to the position i want it to go.
 

The Inherit id where i want the field to go is:
<field name="inherit_id" ref="product.product_template_form_view"/>

 

Whereas the field I want to move is in:
product.template.product.form
product.template
product.product_template_only_form_view

 

Error details:
Element '<field name="default_code">' cannot be located in parent view

 

I tried this and got no errors, but the internal reference is showing twice, and the second field (the new one) is blank

        <record id="view_product_supplier_inherit" model="ir.ui.view">
            <field name="name">product.template.supplier.form.inherit</field>
            <field name="model">product.template</field>

            <field name="priority">99</field>
            <field name="inherit_id" ref="product.product_template_only_form_view"/>
            <field name="arch" type="xml">
                <field name="default_code" position="replace"/>
                <label for="default_code" position="replace"/>
            </field>

            <field name="priority">99</field>
            <field name="inherit_id" ref="product.product_template_form_view"/>
            <field name="arch" type="xml">
                <div name="options" position="inside">
                    <div>
                        <label for="default_code"/>
                        <field name="default_code"/>
                    </div>
                </div>
            </field>
        </record>

 

 

:: EDIT ::

In response to your comment - yes I have tried that, and get 'not located in parent view' error. I have tried this:

        <record id="view_product_template_form" model="ir.ui.view">
            <field name="name">product.template.inherit</field>
            <field name="model">product.template</field>
            <field name="priority">99</field>
            <field name="inherit_id" ref="product.product_template_form_view"/>
            <field name="arch" type="xml">
                <xpath expr="//page[@string='Information']" position="inside">                   
                    <label for="default_code" position="replace"/>
                    <field name="default_code" position="replace"/>
                </xpath>
                <div name="options" position="inside">
                    <div>
                        <label for="default_code"/>
                        <field name="default_code"/>
                    </div>
                </div>
            </field>
        </record>

And the original still shows up, the duplicated field shows too though, which is a start, but unfortunately, neither stay updated when entering values (enter value, save, leave form, return to form, value not there).

Also - with the latest xml i posted (above) neither fields show a tooltip when developer mode is activated and the mouse is hovered over them.

Avatar
Discard

have you tried to inherit from product.product_template_form_view instead of product.product_template_only_form_view ?