Skip to Content
Menu
This question has been flagged

Hi all,

I get this Error Message:

 vlabel(<NewId 0x7fa1c8069a90>,).product_value

when trying to create a new "product" in my custom module.  Any help is appreciated.

See the code here:

sequence.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">

<!-- Sequences for sale.order -->
<record id="seq_vlabel_product" model="ir.sequence">
<field name="name">Product Sequence</field>
<field name="code">vlabel.product.sequence</field>
<field name="prefix">V</field>
<field name="padding">5</field>
<field name="company_id" eval="False"/>
</record>

</data>
</odoo>

label_products.py
# -*- coding: utf-8 -*-

from odoo import models, fields, api, _
from odoo.exceptions import ValidationError

class VLabel(models.Model):
_name = 'vlabel'
_inherit = ['mail.thread.cc', 'mail.activity.mixin']
_rec_name = 'product_name'

@api.constrains('product_fee')
def check_fee(self):
for rec in self:
if rec.product_fee < 68:
raise ValidationError(_('The Price must be at least 68 €'))

@api.depends('product_fee')
def set_value_group(self):
for rec in self:
if rec.product_fee:
if rec.product_fee <= 250:
rec.product_value = 'decent'
else:
rec.product_value = 'great'

product_name = fields.Char(string='Name', required = True)
product_category = fields.Selection([
('vegan', 'Vegan'),
('vegetarian', 'Vegetarian'),
], string="Category")
product_fee = fields.Integer('Price', track_visibility="always")
product_creator = fields.Char(string='Creator')
product_sequence = fields.Char(string='Order Reference', required=True, copy=False, readonly=True,
index=True, default=lambda self: _('New'))
product_value = fields.Selection([
('great', 'Great'),
('decent', 'Decent'),
], string="Value", compute='set_value_group')

@api.model
def create(self, vals):
if vals.get('product_sequence', _('New')) == _('New'):
vals['product_sequence'] = self.env['ir.sequence'].next_by_code('vlabel.product.sequence') or _('New')
result = super(VLabel, self).create(vals)
return result

label_products_view.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>

<!-- Add Search View-->
<record id="label_product_search_view" model="ir.ui.view">
<field name="name">product_search_view</field>
<field name="model">vlabel</field>
<field name="arch" type="xml">
<search string="Search Products">
<field name="product_category" filter_domain="['|', ('product_name','ilike',self), ('product_sequence','ilike',self)]"/>
<field name="product_name"/>
<field name="product_sequence"/>

<!-- Added Filter-->
<separator/>
<filter name="vegetarian" string="Vegetarian" domain="[('product_category','=','vegetarian')]"/>
<filter name="vegan" string="Vegan" domain="[('product_category','=','vegan')]"/>

<!-- Added Group-->
<group string="Group By" expand="0">
<filter string="Category" name="product_category" context="{'group_by':'product_category'}" />
</group>
</search>
</field>
</record>


<!-- Product Form View -->
<record id="vlabel_view" model="ir.ui.view">
<field name="name">vlabel.form.view</field>
<field name="model">vlabel</field>
<field name="arch" type="xml">
<form>
<sheet>
<div class="oe_title">
<h1>
<field name="product_sequence" readonly="1"/>
</h1>
</div>
<group string="Product Information">
<group>
<field name="product_name"/>
<field name="product_category" widget="radio"/>
</group>
<group>
<field name="product_fee"/>
<field name="product_creator"/>
</group>
<group>
<field name="product_value"/>
<field name="product_sequence"/>
</group>
</group>
</sheet>

<!-- Chatter View -->
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers"/>
<field name="activity_ids" widget="mail_activity"/>
<field name="message_ids" widget="mail_thread" options="{'post_refresh': 'recipients'}"/>
</div>
</form>
</field>
</record>

<!-- Product Tree View -->
<record id="label_tree_view" model="ir.ui.view">
<field name="name">label.tree.view</field>
<field name="model">vlabel</field>
<field name= "arch" type = "xml">
<tree string="Product Information">
<field name="product_name"/>
<field name="product_category"/>
<field name="product_value"/>
<field name="product_fee"/>
<field name="product_sequence"/>
<field name="product_creator"/>
</tree>
</field>
</record>


<record id="action_product" model="ir.actions.act_window">
<field name="name">Product</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">vlabel</field>
<field name="view_mode">tree,form</field>
</record>

<menuitem id="vlabel_root" name = 'V-Label' sequence = '0'/>
<menuitem id="vlabel_product" name = "Product" parent = 'vlabel_root' action = 'action_product'/>

</odoo>
Avatar
Discard

Do you really mean to override the create method?

Author

Hm, I basically followed this tutorial https://www.youtube.com/watch?v=Mg80GxrKDOc&list=PLqRRLx0cl0hoJhjFWkFYowveq2Zn55dhM&index=11 and the error kept occuring after adding the compute function. I am very new to this so I am not entirely sure if I meant what I did

Author

In any case, i gave default value for product_fee 1 which helped, so i guess I'm good

Best Answer

Hi Adrian,

Give a try with below code

Change method "set_value_group" as below

 @api.depends('product_fee')
 def set_value_group(self): 
       for rec in self:
             rec.product_value = 'great'
             if rec.product_fee: 
                 if rec.product_fee <= 250:
                     rec.product_value = 'decent'


Thanks

Avatar
Discard
Author

Hey Kanak,

thank you very much, worked like a charm

Best Answer

I accidently flagging the best answer, how to make it back to display in answer list?.. tq

Avatar
Discard
Related Posts Replies Views Activity
0
Apr 20
4089
4
Oct 20
6604
3
Feb 24
8439
1
Nov 23
906
1
Jun 23
1787