Ayuda

0

Adding a new field to the existing model and setting that field value by python method return

Avatar
Raitis

I want to create a new field inside a existing model(product.product) to store there a path for a GIF image. However, field is not being added, and method is not being called. I have checked the fields inside a technical settings and in product.product model itself. Field 'gifpath' is nowhere to be found.

When I try to access that field by xml, new  html tag is created with my record, and field inside it.

New html code:

<record id="image_1024" model="product.product" data-oe-id="1330" data-oe-xpath="/data/xpath/record" data-oe-model="ir.ui.view" data-oe-field="arch">
		<field name="image_1024">gifpath()</field>
	      </record>

Python code(models.py)

# -*- coding: utf-8 -*-

from odoo import models, fields, api


class imgtogif_r(models.Model):
    _inherit = 'product.product'
    field_gifpath = fields.Char('gifpath')

    @api.one
    def gifpath(self,productid):
        selected_prod = self.env['product.product'].search[(`id`, `=`, productid)]
        if selected_prod != None:
            return selected_prod.gifpath

templates.xml code:

<odoo>

    <data>

       <template id="website_sales_inh" name="webiste_sales_inherit" inherit_id="website_sale.products_item">

          <xpath expr="//span[@class='d-flex h-100 justify-content-center align-items-center']" position="replace">
              <record id="image_1024" model="product.product">
        <field name="image_1024">gifpath()</field>
          </record>
              <span t-field="product.image_1920"
                        t-options="{'widget': 'image', 'preview_image': 'image_1024' if product_image_big else 'image_256'}"
                        class="d-flex h-100 justify-content-center align-items-center"/>
          </xpath>

       </template>

    </data>

</odoo>


Avatar
Descartar