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

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.

Related

Domain in Many2one Odoo 11

I have three models
class ZohoTags(models.Model):
_name = 'zoho.tags'
name = fields.Char(string="Tags")
tag_id = fields.Char(string="Tag Id")
class TagsLine(models.Model):
_name = 'zoho.tags.line'
x_zoho_tags = fields.Many2one('zoho.tags', string='Tags')
x_tags_option = fields.Many2one('zoho.tag.option', string='Tags Option', domain="[('tag_ids', '=', x_zoho_tags.tag_id)]")
rules_id = fields.Many2one('hr.salary.rule')
class TagOptions(models.Model):
_name = 'zoho.tag.option'
name = fields.Char(string="tag option name")
option_tag_id = fields.Char(string="tag option id")
tag_ids = fields.Char(string="tag_id")
In the zoho.tags model, I have a field called tag_id and in the zoho.tag.option, I have tag_ids and both are having the same values.
In the zoho.tags.line model, I have a Many2one field called x_zoho_tags, which shows a list of tags like: division, state, etc. and x_tags_option, which shows options for each tag, such as:
Tag (division) has options (A,B,C) and these options are having the same tag_ids stored for (division) tag
I want to add a domain to x_tags_option in order to filter x_tag_option
to show only options that are having the same tag_id.
So when I select division from x_zoho_tags, then x_tags_option should show only A, B and C.
I have tried to add this line below, but it does not work
domain="[('tag_ids', '=', x_zoho_tags.tag_id)]
I have figure it out. This is how I have done it:
In python:
#api.onchange('x_zoho_tags')
def onchange_tags(self):
res = {}
if self.x_zoho_tags:
res['domain'] = {'x_tags_option': [('tag_ids', '=', self.x_zoho_tags.tag_id)]}
return res
In XML:
<field name="x_zoho_tags"/>
<field name="x_tags_option" onchange="onchange_tags(x_zoho_tags)"/>

retrieve values from the same record from a field odoo 10

I will want to retrieve different values ​from a chosen field. Let me explain:
I have this class:
class SchoolWebServices(models.Model):
_name = 'ecole.webservices'
name = fields.Char(string='Nom')
code_produit = fields.Char(string='Produit')
code_CDG = fields.Char(string='Centre de Gestion')
code_Catalog = fields.Char(string='Catalogue Produits')
I have this other class:
class ResPartner_school(models.Model):
_name = 'ecole.partner.school'
_order = 'id desc'
half_pension_name = fields.Many2one(comodel_name="ecole.webservices",
string="Lieu")
And I have a function who is in the class: ecole.partner.school
#api.multi
def create_compte_cantine(self):
print "Inscription réussie"
get_halfpension_name = self.half_pension_name.id
if get_halfpension_name:
code_Catalog = self.env['ecole.webservices'].code_Catalog
I get the id of half_pension_name in my variable get_halfpension_name but I wish to recover the code_Catalog of the same recording too. How to do?
You just need to use dot-notation to retrieve the value:
#api.multi
def create_compte_cantine(self):
self.ensure_one()
if self.half_pension_name:
code_Catalog = self.half_pension_name.code_Catalog
Try to stay in the "rules" of the Odoo guideline. For example a Many2one relation field should be end with _id -> half_pension_id = fields.Many2one(comodel_name="ecole.webservices", string="Lieu")

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.

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

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.

Get field value of inherited model Odoo 8

Hello to all I have been developing module under Odoo 8. I have a class "hrEmployee" with "_inherit=hr.employee" , now in my hrEmployee there is a One2many field having relation with another model "hr.employee.visa". I want to get the field values of the "hrEmployee" with onchange function defined on the field of "hr.employee.visa". Like when I change field value of "hrEmployee", I can get the field value entered on the current form (hrEmployee). How am I able to achieve this in Odoo v8? My Python code is shown below:
class hrEmployee(models.Model):
_inherit = "hr.employee"
diwan_no = fields.Char('Diwan No', size=30, help='Diwan Number')
zeo_number = fields.Char('ZEO Number',size=30, help='ZEO Number')
visas_ids = fields.One2many('hr.employee.visas', 'employee_id', 'Visas')
class hr_employee_visas(models.Model):
_name='hr.employee.visas'
employee_id = fields.Many2one("hr.employee.visas", "Employee" )
#api.onchange('visas_number')
#api.depends( 'visas_number')
def _visa_num(self):
cr=self._cr
uid=self._uid
ids=self._ids
for id in ids:
obj1=self.pool.get('hr.employee').browse(cr,uid,id,context=None)
print obj1.name_related
visas_sponsor = fields.Char('Sponsor')
visas_states = fields.Selection([('apply','Apply'),('active','Active'),('expire','Expire'),('cancel','Cancelled')], string='State' )
visas_number = fields.Char('Visa No', help='Visa Number')
I tried to use self.pool.get browse but it gives me "False" . Plz guide me or point me my mistake. Hopes for suggestion
Try following,
class hr_employee_visas(models.Model):
_name='hr.employee.visas'
employee_id = fields.Many2one("hr.employee", "Employee" )
#api.onchange('visas_number')
#api.depends( 'visas_number')
def _visa_num(self):
for obj in self:
print obj.employee_id.name
Here is the mistake
employee_id = fields.Many2one("hr.employee.visas", "Employee" )
You need to set hr.employee here.
No need to write both of the decorators together, in case of any changes into the visas_number field this method will be called, you can use any of the single decorator for this.

Categories

Resources