Skip to Content
Menu
This question has been flagged
3 Replies
4437 Views

i have inherited the model' product.template' and added a new page UOM . i created a one2many field of  'product.uom'  and added all its field to the page UOM that i have created, so that i could create multiple unit of measure for a single product.

i have also created a function that creates the category_id same as that of product name.


from odoo import models, fields,api
class ProductTemplate(models.Model):
    _inherit = "product.template"
    
    product_uom_ids= fields.One2many('product.uom' ,'product_template_id', string='unit of measure')

    @api.model
    def create(self, vals):
        new=self.env['product.uom.categ'].create({'name': vals['name']})
        if vals['name']:
            for record in vals['product_uom_ids']:
                record[2]['category_id']=new.id
        return super(ProductTemplate, self).create(vals)
    
    @api.multi
    def write(self,values):
        if values:
            print(values)
            print(values['product_uom_ids'][0][1])
            list=self.env['product.uom'].search([('id','=',values['product_uom_ids'][0][1])],limit=1)
            print(list)
            for item in values['product_uom_ids']:
               
                print(list.category_id.name)
              
            for record in values['product_uom_ids']:
                if record[1]==False:
                    record[2]['category_id']=list.category_id.id
        return super(ProductTemplate, self).write(values)


class ProductTemplateid(models.Model):
    _inherit = "product.uom"
    product_template_id=fields.Many2one('product.template')


But when i edit the name of the product and tried to save i'am getting an error.

Traceback (most recent call last):
  File "/home/zinfog/Documents/wrk/odoo/odoo/http.py", line 642, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/zinfog/Documents/wrk/odoo/odoo/http.py", line 684, in dispatch
    result = self._call_function(**self.params)
  File "/home/zinfog/Documents/wrk/odoo/odoo/http.py", line 334, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/home/zinfog/Documents/wrk/odoo/odoo/service/model.py", line 101, in wrapper
    return f(dbname, *args, **kwargs)
  File "/home/zinfog/Documents/wrk/odoo/odoo/http.py", line 327, in checked_call
    result = self.endpoint(*a, **kw)
  File "/home/zinfog/Documents/wrk/odoo/odoo/http.py", line 942, in __call__
    return self.method(*args, **kw)
  File "/home/zinfog/Documents/wrk/odoo/odoo/http.py", line 507, in response_wrap
    response = f(*args, **kw)
  File "/home/zinfog/Documents/wrk/odoo/addons/web/controllers/main.py", line 892, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/home/zinfog/Documents/wrk/odoo/addons/web/controllers/main.py", line 884, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/home/zinfog/Documents/wrk/odoo/odoo/api.py", line 689, in call_kw
    return call_kw_multi(method, model, args, kwargs)
  File "/home/zinfog/Documents/wrk/odoo/odoo/api.py", line 680, in call_kw_multi
    result = method(recs, *args, **kwargs)
  File "/home/zinfog/Documents/wrk/odoo/myaddons/product_category/model/category.py", line 23, in write
    print(values['product_uom_ids'][0][1])


Avatar
Discard

What are you getting when printing values?

print(values)

Author Best Answer

this what i got when print(values) :

('values:', {u'name': u'newprodu'})

i tried this code:
def write(self,values):
if self.name in values:

for product_uom in self.product_uom_ids:
print "product_uom:",product_uom.category_id.name
print "name:",self.name
product_uom.category_id.name = self.name
list=self.env['product.uom'].search([('id','=',values['product_uom_ids'][0][1])],limit=1)
print("list",list)
for item in values['product_uom_ids']:
# print(item)
print(list.category_id.name)
#when adding a new unit of measure its category will be empty so only that empty record gets the category id
for record in values['product_uom_ids']:
if record[1]==False:
record[2]['category_id']=list.category_id.id
return super(ProductTemplate, self).write(values)

now i can edit the product name and save but when i edit the product and try to add new unit of measure i' am getting an error:

Avatar
Discard
Best Answer

print(values['product_uom_ids'][0][1])

this is wrong in write(), because values doesn't contains any product_uom_ids details without any changes on one2many

So you please try 

if 'name' in values:

    for product_uom in self.product_uom_ids:
        product_uom.your_fields = your_value_toupdate

Avatar
Discard
Related Posts Replies Views Activity
2
Oct 23
4750
2
Nov 22
5164
0
Sep 21
1778
4
Aug 20
6581
2
Mar 20
9692