Store value (onchange) - new api [Odoo/Openerp] - python

I added 2 fields in 'sale.order.line' object. Let's say 'field1' and 'field2', those are readonly field. The value of the 2 fields will appear whenever the product is change in order line.
When I select a product, it shows the value of the two fields but when save it, the value will back 0, not stored.
Here's my code:
class sale_order_line(models.Model):
_inherit = 'sale.order.line'
field1 = fields.Float('One')
field2 = fields.Float('Two')
#api.multi
def product_id_change(self, pricelist, product, qty=0,
uom=False, qty_uos=0, uos=False, name='', partner_id=False,
lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False):
res = super(sale_order_line, self).product_id_change(pricelist, product, qty,
uom, qty_uos, uos, name, partner_id,
lang, update_tax, date_order, packaging, fiscal_position, flag)
if product:
one = self.search([('product_id', '=', product), ('partner_id', '=', partner_id)])
two = self.search([('product_id', '=', product), ('partner_id', '!=', partner_id)])
if customer:
field1 = one[-1]
res['value']['field1'] = field1
if other:
field2 = two[-1].
res['value']['field2'] = field2
return res

In Odoo framework we are now allowing to set readonly fields value, as well as readonly fields will not be getting in vals in create and write methods.
So if you set those readonly fields value in onchange methods then also it will not persist it's value because by nature it's readonly, it will not gives you any errors.
Purpose : The aims behind to define readonly attributes is to behave same through the all states of the record on UI and user can not change it's value and mainly define for display purpose.That is why readonly fields are not accessible for edit in onchange method.
Solution:
You need to override CREATE or WRITE method in following manner.
#api.model
def create(self, vals):
######
# WRITE YOUR LOGIC HERE AND BRING THOSE VALUE INTO VARIABLE AND THEN UPDATE IT IN VALS
VARIABLE_1 = SEARCH YOUR DATA
VARIABLE_2 = SEARCH YOUR DATA
vals.update({field1' : VARIABLE_1, 'field_2' : VARIABLE_2})
return super(sale_order_line, self).create(vals)
Update vals (record dictionary) by setting those readonly fields in to dictionary before calling super method or update those fields after calling super method.
There is an alternative solution for your problem. In Odoo Apps one module available from that system will store readonly value in the database.
Read Only ByPass

It is because readonly mode works only for display. In this case value from fields will not send to server-side.
You can override method create of sale.order.line. It should be something like this:
class sale_order_line(models.Model):
_inherit = 'sale.order.line'
#api.model
def create(self, vals):
# just example
vals[u'field1'] = 2.03
vals[u'field2'] = 3.05
return super(sale_order_line, self).create(vals)
Hope this helps you.

Related

Create a new record in another model with a condition in Odoo v8

I have this model
class crm_claim(models.Model):
_inherit = 'crm.claim'
needs_credit_note = fields.Boolean(string="Needs Credit Note?")
partner_id = fields.Many2one('res.partner', string='Partner')
I need to create a function that, when needs_credit_note == True,creates a new record con account.invoice with the same partner_id, and when needs_credit_note == False, delete this record. This is what I did but is not working yet.
#api.depends("needs_credit_note")
def create(self, vals):
self.ensure_one()
if self.needs_credit_note:
self.env['account.invoice'].create({
'partner_id': self.partner_id.id,
})
You can't do that, self is an empty record set, and self.needs_credit_note will be evaluated to False. You need to call super or get the values from vals.
The partner field should be required when the needs_credit_note field value is set
Example:
#api.model
def create(self, vals):
record = super(crm_claim, self).create(vals)
if record.needs_credit_note:
self.env['account.invoice'].create({
'partner_id': record.partner_id.id,
})
return record
Probably, you will need to provide the required fields to create the invoice record

Retrieve field values in a compute of a relationship field odoo 10

I would like to retrieve all the values ​​of the records of a model in a Many2one field. I don't know if it's possible with a compute.
My class of I want to recover the value:
class ResPartner_school(models.Model):
_name = 'ecole.partner.school'
_order = 'id desc'
school_name = fields.Many2one(comodel_name="ecole.establishment.webservice",
string="Etablissement Scolaire",
default=1)
school_level = fields.Many2one(comodel_name="ecole.establishment.webservice",
string="Niveau Scolaire",
compute="_get_level")
My other class:
class SchoolEstablishmentWebServices(models.Model):
_name = 'ecole.establishment.webservice'
_rec_name = "establishment_name"
establishment_name = fields.Many2one(comodel_name="horanet.school.establishment", string="Etablissement Scolaire")
id_establishment = fields.Char(string='idEtablissement')
grade_name = fields.Many2one(comodel_name="horanet.school.grade", string="Niveau Scolaire")
id_class = fields.Char(string='idClasse')
My function in my class ResPartner_school:
#api.multi
def _get_level(self):
school_level = self.school_name.grade_name
return school_level
How to retrieve all values ​​from the grade_name field of the class SchoolEstablishmentWebServices?
One solution to show "all" data (depends on how much of data there is) is overriding the name_get() of a model with two variants.
Variant context-based name_get()
First override the name_get() of model ecole.establishment.webservice
class SchoolEstablishmentWebServices(models.Model):
_name = 'ecole.establishment.webservice'
#api.multi
def name_get(self):
res = []
for webservice in self:
if 'full_name' in self.env.context:
res.append((webservice.id, webservice.get_full_name()))
else:
res.append((webservice.id, webservice.establishment_name))
return res
def get_full_name(self):
# example logic
self.ensure_one()
full_format = "{establishment}, {grade}"
return full_format.format(
establishment=self.establishment_name, grade=self.grade_name)
Then you need to put the value full_name into the context. You could add it to the field itself, which won't work that good. Better you put that value into the context of the action of the menu, which is used to show your 'ecole.partner.school' entries.
<record id="my.list.action.for.school" model="ir.actions.act_window">
<field name="name">my.action</field>
<!-- and so on -->
<field name="context">{'full_name': 1}</field>
</record>
Variant non context-based -> it's the same as variant 1 without using the context at all. It will be system-wide at not only where you want it to be. It's the easier variant.

ODOO 10 many2many

I have a problem in ODOO 10 with a many2many relationship.
I have extended res.partner this way:
class ResPartner(models.Model):
x_bagsdesign = fields.Many2many('product.product',string='Bags Design',relation='bags_design_manufactur')
then I have extended also product.template model:
class product_template_fields(models.Model):
_inherit = 'product.template'
x_traders_stock = fields.Many2many(
'res.partner', string='Traders with access to stock',relation='xtradstock_res_partner_rel')
#api.multi
def write(self, vals):
record = super(product_template_fields, self).write(vals)
for singletrader in self.x_traders_stock:
singletrader.x_bagsdesign = [(4,self.id)]
return record
This way every time a new x_traders_stock is inserted in product.template, a new x_bags_design is also created in res.partner.
BUT.. when I save a new record in product.template I get an sql error:
bad query: INSERT INTO bags_design_manufactur (res_partner_id, product_product_id)
(SELECT a, b FROM unnest(ARRAY[1]) AS a, unnest(ARRAY[7]) AS b)
EXCEPT (SELECT res_partner_id, product_product_id FROM bags_design_manufactur WHERE res_partner_id IN (1))
I don't understand where the EXCEPT part of the sql query is coming from and how to prevent it. If anyone can help I would be grateful.. thanks!
The error message is a bit different from expected, but I can fix some problems in your code. First of all you have to take into account that a product.product object is a variant of a product.template object, so you can have in a database many product.product objects pointing to the same product.template (e.g. a product.template is a T-shirt and a product.product is a T-shirt colour red size M). This means that you can't try to set the ID of a product.template in a field which is expecting the ID of a product.product, as you're doing here:
singletrader.x_bagsdesign = [(4,self.id)]
Of course, that mistake will not give you the message error you are receiving, there must be something wrong in other part of your code (I guess related to bags_design_manufactur model).
However, to fix the problem I told you above, you should write this:
class product_template_fields(models.Model):
_inherit = 'product.template'
x_traders_stock = fields.Many2many(
comodel_name='res.partner',
string='Traders with access to stock',
relation='xtradstock_res_partner_rel'
)
#api.multi
def write(self, vals):
result = super(product_template_fields, self).write(vals)
for prod_templ in self:
products = self.env['product.product'].search([
('product_tmpl_id', '=', prod_templ.id),
])
for singletrader in prod_templ.x_traders_stock:
singletrader.write({
'x_bagsdesign': [(4, product.id) for product in products],
})
return result
EDIT
product.product inherits from product.template by delegation, this means that every field you create in product.template model is going to be available in product.product objects, so when you are creating the Many2many field x_traders_stock in product.template, you're creating it in product.product too, so you don't need to add records each time a x_trader is generated. Instead you should change your models:
class ResPartner(models.Model):
x_bagsdesign_prod_templ = fields.Many2many(
comodel_name='product.template',
column1='partner_id',
column2='product_tmpl_id',
string='Bags Design',
relation='xtradstock_res_partner_rel'
)
class ProductTemplate(models.Model):
_inherit = 'product.template'
x_traders_stock = fields.Many2many(
comodel_name='res.partner',
column1='product_tmpl_id',
column2='partner_id',
string='Traders with access to stock',
relation='xtradstock_res_partner_rel'
)
And then, if you want to access to the product.product objects a partner has, you can do it this way:
any_partner.x_bagsdesign_prod_templ.mapped('product_variant_ids')
If you preferred it, you could even create a new related field in res.partner which brought the product.product objects a partner has.

Filter Many2one field - Odoo v8

I know I can filter Many2one fields, from python code, or even xml views, with the domain flag, but I have a slightly different scenario right now,
Consider having a model like this:
class MyModel(models.Model):
_name = 'mymodel'
fieldsel = fields.Selection([('sheet', 'Sheet'),('reel','Reel')], string='Printing PPT Type',
track_visibility='onchange', copy=False,
help=" ")
fieldmany = fields.Many2one('text.paper', string="Text Paper")
The text.paper model has another Selection field, which has the same values as fieldsel, however, I cannot use domain since it will filter every text.paper statically.
My issue is, that I need to filter text.paper depending on which option I choose from fieldsel, so, let's say text.paper looks something like this:
class text_paper(models.Model):
_name = 'text.paper'
name = fields.Char(string="Code")
paper_type = fields.Selection([('sheet', 'Sheet'),('reel','Reel')], string="Paper Type")
I need to filter from mymodel the text.paper depending on the fieldsel field, if reel selected, filter text.paper which are reel, and if sheet selected, filter text.paper accordingly.
I hope I've explained myself.
Any ideas?
what you need is dynamic domain for many2one you can achive this by onchange event
class MyModel(models.Model):
_name = 'mymodel'
....
...
#api.onchange('fieldsel ')
def change_domain(self):
"""change the domain of fieldmany whenever the user changes the selected value."""
self.fieldmany = False # may be you want to reset the value when the user changes the selected value
if self.fieldsel : # make sure the user has selected a value
return {'domain': {fieldmany: [('paper_type', '=', self.fieldsel)]}}
else: # remove domain
return {'domain': {fieldmany: []}}

use existing field as _id using elasticsearch dsl python DocType

I have class, where I try to set student_id as _id field in elasticsearch. I am referring persistent example from elasticsearch-dsl docs.
from elasticsearch_dsl import DocType, String
ELASTICSEARCH_INDEX = 'student_index'
class StudentDoc(DocType):
'''
Define mapping for Student type
'''
student_id = String(required=True)
name = String(null_value='')
class Meta:
# id = student_id
index = ELASTICSEARCH_INDEX
I tied by setting id in Meta but it not works.
I get solution as override save method and I achieve this
def save(self, **kwargs):
'''
Override to set metadata id
'''
self.meta.id = self.student_id
return super(StudentDoc, self).save(**kwargs)
I am creating this object as
>>> a = StudentDoc(student_id=1, tags=['test'])
>>> a.save()
Is there any direct way to set from Meta without override save method ?
There are a few ways to assign an id:
You can do it like this
a = StudentDoc(meta={'id':1}, student_id=1, tags=['test'])
a.save()
Like this:
a = StudentDoc(student_id=1, tags=['test'])
a.meta.id = 1
a.save()
Also note that before ES 1.5, one was able to specify a field to use as the document _id (in your case, it could have been student_id), but this has been deprecated in 1.5 and from then onwards you must explicitly provide an ID or let ES pick one for you.

Categories

Resources