How to create a relation fields in odoo? - odoo

There are no proper examples on how to create relational fields in openerp like one2many, many2many, many2one and one2one. So can anyone show me with example on sales module.

Might helps you,
_columns = {
'current_rate': fields.related('company_currency_id','rate_silent', type='float', relation='res.currency',digits_compute=dp.get_precision( 'Account'), string='Current Rate', readonly=True),
}
Here,
company_currency_id => the field in the same model through which the new field will be relate,
rate_silent => is the field which you are going to relate with new field, means the field from source model,
relation => is the source model name,
type => is the datatype of the source field
Note : When you update value in your newly defined related field it
will be updated in source field as well, though it's always advisable
to set readonly in newly defined field.

I don't have particular example of sales module. But you can get the general idea from below syntax.It would be helpful for any kind of application.
class openerp.fields.Many2one(comodel_name=None, string=None, **kwargs)
Bases: openerp.fields._Relational
The value of such a field is a recordset of size 0 (no record) or 1 (a single record).
Parameters
comodel_name -- name of the target model (string)
domain -- an optional domain to set on candidate values on the client side (domain or string)
context -- an optional context to use on the client side when handling that field (dictionary)
ondelete -- what to do when the referred record is deleted; possible values are: 'set null', 'restrict', 'cascade'
auto_join -- whether JOINs are generated upon search through that field (boolean, by default False)
delegate -- set it to True to make fields of the target model accessible from the current model (corresponds to _inherits)
The attribute comodel_name is mandatory except in the case of related fields or field extensions.
=======================================================================
class openerp.fields.One2many(comodel_name=None, inverse_name=None, string=None, **kwargs)
Bases: openerp.fields._RelationalMulti
One2many field; the value of such a field is the recordset of all the records in comodel_name such that the field inverse_name is equal to the current record.
Parameters
comodel_name -- name of the target model (string)
inverse_name -- name of the inverse Many2one field in comodel_name (string)
domain -- an optional domain to set on candidate values on the client side (domain or string)
context -- an optional context to use on the client side when handling that field (dictionary)
auto_join -- whether JOINs are generated upon search through that field (boolean, by default False)
limit -- optional limit to use upon read (integer)
The attributes comodel_name and inverse_name are mandatory except in the case of related fields or field extensions.
==========================================================================
class openerp.fields.Many2many(comodel_name=None, relation=None, column1=None, column2=None, string=None, **kwargs)
Bases: openerp.fields._RelationalMulti
Many2many field; the value of such a field is the recordset.
Parameters----------------------------------------------------------------
comodel_name -- name of the target model (string)
The attribute comodel_name is mandatory except in the case of related fields or field extensions.
Parameters
relation -- optional name of the table that stores the relation in the database (string)
column1 -- optional name of the column referring to "these" records in the table relation (string)
column2 -- optional name of the column referring to "those" records in the table relation (string)
The attributes relation, column1 and column2 are optional. If not given, names are automatically generated from model names, provided model_name and comodel_name are different!
Parameters
domain -- an optional domain to set on candidate values on the client side (domain or string)
context -- an optional context to use on the client side when handling that field (dictionary)
limit -- optional limit to use upon read (integer)

Related

OneToMany field in Odoo proper way to link two models

raise UserError(_("No inverse field %r found for %r") % (self.inverse_name, self.comodel_name))
odoo.exceptions.UserError: ("No inverse field None found for 'res.sector'", '' )
I'm trying to create a new model and having problems linking it to another model.
You probably declared a One2many field like following:
field_name = fields.One2many('res.sector')
You need to specify the inverse name (mandatory), the name of a Many2one field declared in res.sector that reference the model where you declared the One2many field.
As mentioned above, you need to specify an inverse name on the relation.
For example: (workcenter_id) is the inverse field
equipment_ids = fields.One2many('maintenance.equipment', 'workcenter_id')

related attributes , related fields on inherited view odoo

what is related attribute , what it can be used for ? and how to add a related attribute .
I'm trying to add a related field for total amount.
Related Field
In such case we need to take the value from any other table but we already have the reference of that table in our model, in that case we can utilize one functionality with that we can add the multiple fields from the reference table by just having the only one reference field in our model.
One relational field (Many2one) of the source model is mandatory to
have in the destination model to create relational field.
Consider the company_currency_id field from the res.currency model is there in the destination model then you can get the current rate of that currency in your target model.
syntax: v7
_columns = {
'current_rate': fields.related('company_currency_id','rate_silent', type='float', relation='res.currency',digits_compute=dp.get_precision( 'Account'), string='Current Rate', readonly=True),
}
Here,
company_currency_id => the field in the same model through which the new field will be relate,
rate_silent => is the field which you are going to relate with new field, means the field from source model,
relation => is the source model name,
type => is the datatype of the source field
Syntax: v8 and newer version
current_rate = fields.Float(string='Current Rate', related='company_currency_id.rate_silent', store=True, readonly=True)
Note :
Value is available in that field only after you save the record.
When you update value in your newly defined related field it
will be updated in source field as well, though it's always advisable
to set readonly in newly defined field.
In context of Odoo related field means, that its value will be
read from another table (related table) --> store=False
read from another table, but stored in the table its defined on --> store=True
Examples (Odoo V8):
sale.order currency_id: it is persisted in product_pricelist
stock_quant packaging_type_id: it is persisted in product_packaging and stock_quant. Everytime you change the value on product_packinging it will be updated on stock_quant, too, not vice versa.
**Related Field* is used when you wanted to pull a value from another model you can use related field on fields.
Here is an example for you.
order_id = fields.Many2one(comodel_name='sale.order', 'Sale Order')
order_amount = fields.Monetary(string='Order Amount',
store=True,
related='order_id.amount_total')
you must add a Many2one field in the model which relates to the model you want to access a field. in our case model is sale.order.
with the related kwarg you can relate field of the related model defined in the Many2one field. in our case order_id.
Setting the store kwarg will automatically store the value in database. With new API the value of the related field will be automatically updated.(Reference)
Hope this helps!

access to many2one fields

I have a many2one field like this
state = fields.Many2one ("ags.traffic.operation.state")
state has the following fields
name = fields.Char
sequence = fields.Integer
type = fields.Selection
in my view i have
<field name = "state" widget = "statusbar" clickable = "True" / >
how can i access those fields to set a default value?
if you want to define the field that gets shown in the drop-down in your view in your model define _rec_name this tells odoo to display that field (in a drop down or in a many2many tag field) when a many2one or one2one relationship is created between that model and another model. for example if you want the sequence number to display in the drop down just set
_recname = 'sequence'
but by default odoo checks the model's field and if it finds a name field (just like you have defined in your model). it uses that as the default display name.
if you want to search records in odoo you can use the search method. please see the documentation for more information about the odoo ORM
https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.browse
search(args[, offset=0][, limit=None][, order=None][, count=False])
but a typical example is
search_records = self.env['your.model'].search([('id', 'in', ids)])

Auto Populate a Grid in Odoo and Error

Odoo do an Auto Populate a Grid when a user Creating a new information with One2many relations fields?
this is my Example Auto Populate
def getCheckListId(self):
self.env.cr.execute("select 1 employee_id,1 PARAM1,1 PARAM2,1 PARAM3,1 PARAM3,1 PARAM4 from hr_employee_checklist ")
checklistTemplates = self.env.cr.fetchall()
return checklistTemplates
And this function will be used as a default in One2ManyFields
employee_checklists = fields.One2many('hr.employee_checklist','employee_id', readonly=False,copy=False, default = getCheckListId)
But I have an error
the error is
AttributeError: 'str' object has no attribute 'iteritems'
Can someone help me with this problem or other ways to populate Grid in Odoo
One2many
One2many field; the value of such a field is the recordset of all the records in comodel_name such that the field inverse_name is equal to the current record.
Parameters
comodel_name -- name of the target model (string)
inverse_name -- name of the inverse Many2one field in comodel_name
(string)
domain -- an optional domain to set on candidate values on the client
side (domain or string)
context -- an optional context to use on the client side when
handling that field (dictionary)
auto_join -- whether JOINs are generated upon search through that
field (boolean, by default False)
limit -- optional limit to use upon read (integer)
So, One2many fields always contains comodel's reference value, you just need to give ids of that relational field, remaining things will maintain by odoo engine it self.
#api.model
def getCheckListId(self):
return self.env['hr.employee.checklist'].search([]).ids

How to create fungsion button save, from one2many to one2many other?

I try create one2many which take from ony2many other with button save, this for version odoo. It is possible, and if yes, how to do that.
Thanks for your contribution.
Regards.
No that was not Possible
syntax :
class openerp.fields.One2many(comodel_name=None, inverse_name=None, string=None, **kwargs)[source]
One2many field; the value of such a field is the recordset of all the records in comodel_name such that the field inverse_name is equal to the current record.
Parameters
comodel_name -- name of the target model (string)
> inverse_name -- name of the inverse Many2one field in comodel_name
(string)
domain -- an optional domain to set on candidate values on the client
side (domain or string)
context -- an optional context to use on the client side when handling
that field (dictionary)
auto_join -- whether JOINs are generated upon search through that
field (boolean, by default False)
limit -- optional limit to use upon read (integer)
The attributes comodel_name and inverse_name are mandatory except in the case of related fields or field extensions.