Odoo Inheriting fields and functionality - odoo

I am have made two different classes(model.Models). I have 10 fields in one class and I need to inherit only two fields in other class from the former one. Like I update two fields in one class , It automatically must be updated in next class. So please help me out. How should I need to inherit these two fields and their functionality?
This is the Odoo Framework.

You can use related fields to get values from another model. You can do this by defining a Many2one to the model and access field in that model using related fields.
example:
class ModelA(models.Model):
_name = 'model.a'
field1 = fields.Char()
.....
field10 = fields.Char()
class ModelB(models.Model):
_name = 'model.b'
model_a_id = fields.Many2one('model.a', string='Related Model')
field1 = fields.Char(related='model_a_id.field1', string='field1')
field2 = fields.Char(related='model_a_id.field2', string='field2')
You can access any field in model.a from model.b using related fields. If you change values of field1 or field2 from any of these models, the change will be reflected in both models. So if you don't want the user to change values of these model from model.b, you can make them readonly in that model.

<record id="ping_tree_view_id" model="ir.ui.view">
<field name="name">Ping tree view</field>
<field name="model">ping.ping</field>
<field name="arch" type="xml">
<tree string="Ping">
<field name="model_a_id"/>
<field name="Last_update"/>
<field name="Last_attempt"/>
</tree>
</field>
</record>
<record id="ping_form_view_id" model="ir.ui.view">
<field name="name">Ping form view</field>
<field name="model">ping.ping</field>
<field name="arch" type="xml">
<form string="Ping">
<sheet>
<group>
<field name="model_a_id"/>
<field name="Last_update"/>
<field name="Last_attempt"/>
</group>
</sheet>
</form>
</field>
</record>
<record model="ir.actions.act_window" id="ping_actions_win">
<field name="name">Ping</field>
<field name="res_model">ping.ping</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>

Related

Odoo 11 - pull fields values from other models

I have added a custom field (many2one) in crm.lead form and a custom class for that
Here is my code (I am using odoo 11):
models.py
from odoo import models, fields, api
# Add fields to Leads
class Leads_events(models.Model):
_inherit = 'crm.lead'
venue = fields.Many2one('crm.lead.venue',String="Venue")
venue_city = fields.Char(String="City")
venue_country = fields.Many2one('res.country', string="Country")
# Create venue class
class Leads_venue(models.Model):
_name = 'crm.lead.venue'
name = fields.Char(string="Venue name")
city = fields.Char(string="City")
country = fields.Many2one('res.country', string="Country")
address = fields.Text(string="Address")
website = fields.Char(string="Website")
phone = fields.Char(string="Phone")
fax = fields.Char(string="Fax")
notes = fields.Text(string="Notes")
views.xml
<record id="crm_lead_venue_form" model="ir.ui.view">
<field name="name">crm.lead.venue.form</field>
<field name="model">crm.lead.venue</field>
<field name="arch" type="xml">
<form string="Venues">
<group>
<group>
<field name="name"/>
<field name="country"/>
<field name="city"/>
<field name="address"/>
</group>
<group>
<field name="phone"/>
<field name="fax"/>
<field name="website"/>
</group>
<group>
<field name="notes" />
</group>
</group>
</form>
</field>
</record>
<record id="crm.crm_case_form_view_oppor_events" model="ir.ui.view">
<field name="name">crm_case_form_view_oppor_events</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_case_form_view_oppor"/>
<field name="arch" type="xml">
<xpath expr="//notebook" position="before">
<group string="Venue">
<field name="venue"/>
<field name="event_city" string="City"/>
<field name="event_country"/>
</group>
</xpath>
</field>
</record>
This works fine. Now what I am trying to pull is the city and country from crm.lead.venue selected record to crm.lead 'venue_city' and 'venue_country' fields.
I looked at onchange function but I cannot find how to make it work...
I've tried this following another post I read but it is not working
#api.onchange('venue')
def onchange_venue(self):
if self.venue:
self.event_city = self.venue.city
self.event_country = self.venue.country
Is there anywhere I can find a documentation or help more complete on this ? The official documentation is not very precise on this matter.
The onchange method works when the user change the field in the view, and I cant see the field on your view, so the method would be never be called. You should add "venue" field to the form and test it again.
Edit
Have you seen that it is not event_city but venue_city? The onchange should be on leads_events. Did you test that onchange is executed and has no value? Or it doesnt even execute the function ?
#api.onchange('venue')
def onchange_venue(self):
if self.venue:
self.venue_city = self.venue.city
self.venue_country = self.venue.country

how to make an odoo field invisible if a many2many field value is "rice"

I am trying to make a duration character field visible 'only if' parent field called category_ids Many2Many field value "Non-Permanent" is selected. I have tried so many things but seems not working. Below is my code snippet
Model:
class hr_employee(models.Model):
_inherit = 'hr.employee'
identification_id = fields.Char(string="Employee No")
duration = fields.Char(string="Duration")
View:
<!-- Employee Form View -->
<record model="ir.ui.view" id="hr_custom_form_view">
<field name="name">hr.custom</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="groups_id" eval="[(4, ref('base.group_hr_user'))]"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='user_id']" position="after">
<field name="duration"/>
</xpath>
</field>
</record>

Inherit form of res.partner

I create a new class fleet_agent that inherits from res.partner without problems
class fleet_agent(osv.Model):
_name = 'fleet.agent'
_inherit = ['res.partner']
_columns = {
'test': fields.char('Test', help='Test'),
}
then fleet_agent_form inherits base.view_partner_form also no problem
<record id="fleet_agent_form" model="ir.ui.view">
<field name="name">fleet.agent.form</field>
<field name="model">fleet.agent</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<field name="state_id" position="before">
<field name="x" />
</field>
</field>
</record>
My problem is that this form does not appear as the formatting of view_partner_form and with all fields without any order ?
You have created a new model 'fleet.agent' by extending the base 'res.partner'. In order to show all the fields, you need to specify the fields in xml or use the classical inheritance by removing 'fleet.agent' and add your field to the base 'res.partner'.
class fleet_agent(osv.Model):
_inherit = 'res.partner'
_columns = {
'field': fields.char('Name', help='help'),
}
In xml file:
<field name="model">fleet.agent</field>
class Partner(model.Model):
_inherit = 'res.partner'
test = fields.Char(string='Test', help='Test'),
}
<record id="fleet_agent_form" model="ir.ui.view">
<field name="name">partner.test</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<field name="test" />
</field>
</record>
Try following,
class fleet_agent(osv.Model):
_inherit = ['res.partner']
_columns = {
'test': fields.char('Test', help='Test'),
}
<record id="fleet_agent_form" model="ir.ui.view">
<field name="name">fleet.agent.form</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<field name="state_id" position="before">
<field name="x" />
</field>
</field>
</record>
Remove _name = 'fleet.agent' from your python code, it's create new model, and in xml when you extend any parent model you must write parent model name into the view
<field name="model">res.partner</field>
So you will get extended functionality with your base class's functionality.
To copy table to your moule you should use inherits keyword instead of inherit. Also you have to copy view definition of partner form of base module and change it, extending wouldn't help you in creating new table.
Source
Update: additional information on ways os inheritance:
Update2: looks like you're on right way: there is a third way of inheritance: with inherit keyword and by specifying new _name for new model.
Use:
class res_partner(models.Model):
_inherit = 'res.partner'
test = fields.Char(string='test')`
Upgrade module from console:
./odoo-bin -u [module_name] -c [configuration_file

ODOO 8 on_change

Please I am facing somes problems with the new odoo 8 api, I have the following classes
class TypeProcessus(models.Model):
_name = 'atom.promaintenance.type.processus'
name = fields.Char()
id_phases = fields.One2many('atom.promaintenance.phases','id_processus','Liste des Phases')
class Phases(models.Model):
_name = 'atom.promaintenance.phases'
name = fields.Char()
autoriserCommentaire = fields.Boolean()
autoriserPiecesJointes = fields.Boolean()
id_processus = fields.Many2one('atom.promaintenance.type.processus')
parent_id = fields.Many2one('atom.promaintenance.phases','Phase Parent', select=True, ondelete='cascade')
commentaire = fields.Text()
#api.one
#api.onchange('name')
def phases_write(self):
print 'test'
<record model="ir.ui.view" id="atom_promaintenance_type_processus">
<field name="name">atom.promaintenance.type.processus.form</field>
<field name="model">atom.promaintenance.type.processus</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Type Processus" >
<sheet>
<h1>UPDATED</h1>
<field name="name" />
<tree string="note_evaluation_tree" editable="bottom">
<field name="id_phases" />
</tree>
</sheet>
</form>
</field>
</record>
First of all my problem is when Creating a new Processus, and adding phases, there is a relation parent child between phases and the drop down list for parent stay empty unless u save the processus to make them available.
i managed to add onChange event to the phases to persist them to database but i can't figure out how to save those records with the new api system, thank you
If you mean what I understand, you need to use the widget one2many_list in the XML code, which by the way, I think is wrong. It should be something like this:
<record model="ir.ui.view" id="atom_promaintenance_type_processus">
<field name="name">atom.promaintenance.type.processus.form</field>
<field name="model">atom.promaintenance.type.processus</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Type Processus" >
<sheet>
<h1>UPDATED</h1>
<field name="name" />
<field name="id_phases" widget="one2many_list">
<tree string="note_evaluation_tree" editable="bottom">
<field name="name"/>
<field name="autoriserCommentaire"/>
<field name="autoriserPiecesJointes"/>
<field name="parent_id"/>
<field name="commentaire"/>
</tree>
</field>
</sheet>
</form>
</field>
</record>
The widget will allow you to add phases for a processus and then save it.
The new API uses self for all the record modifications. So in your case, if you want to change the name, write like this:
#api.one
#api.onchange('name')
def onchange_name(self):
self.name = 'what you want to save'

Add customer field in openerp pos module

I am using openerp 6.1.Here in pos module there is no feature of selecting customer.I want to add this field.But i couldn't.Can anyone help me???
You can add extra fields by inheriting any model in your customized module.
You have to inherit the "pos.order" object & view.
Inherit 'pos.order' in your .py:
class pos_order(osv.osv):
_inherit = 'pos.order'
_columns = {
'customer_id': fields.many2one('res.partner', 'Customer'),
}
pos_order()
Above code will create a field in your database.
Now, To display your field in your form, inherit the view of 'pos.order' in your .xml,:
<record id="inherited_form_pos_view" model="ir.ui.view">
<field name="name">pos.order.form.inherit</field>
<field name="model">pos.order</field>
<field name="type">form</field>
<field name="inherit_id" ref="point_of_sale.view_pos_pos_form"/>
<field name="arch" type="xml">
<field name="name" position="after">
<field name="customer_id"/>
</field>
</field>
</record>
Now, you can see "Customer" field in your pos.order form.