I am trying to inherit res.users in odoo 12. But I am getting mixing apples and oranges error. I want to create a new table having the fields of res.users models.
model.py
class SaleIndividual(models.Model):
_name = 'sale.individual'
_inherit = 'res.users'
individual_description = fields.Char()
view.xml
<odoo>
<record id="view_form_sale_custom_individual" model="ir.ui.view">
<field name="name">Individual Form</field>
<field name="model">sale.individual</field>
<field name="inherit_id" ref="auth_signup.res_users_view_form"/>
<field name="arch" type="xml">
<field name="login" position="after">
<field name="individual_description" />
</field>
</field>
</record>
</odoo>
error:
raise TypeError("Mixing apples and oranges: %s in %s" % (item, self))
TypeError: Mixing apples and oranges: sale.individual(<odoo.models.NewId object at 0x123018048>,) in res.users()
First Method:
If you want t inherit res users then please follow the below steps:
class ResUsers(models.Model):
_inherit = 'res.users'
individual_description = fields.Char()
view.xml
<odoo>
<record id="view_form_sale_custom_individual" model="ir.ui.view">
<field name="name">Individual Form</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="auth_signup.res_users_view_form"/>
<field name="arch" type="xml">
<field name="login" position="after">
<field name="individual_description" />
</field>
</field>
Second Method:
if you want create new model then follow the steps below:
class SaleIndividual(models.Model):
_name = 'sale.individual'
_inherit = 'res.users'
individual_description = fields.Char()
Need to create a new view file with out inheriting res users view file for this case.
<odoo>
<record id="view_form_sale_custom_individual" model="ir.ui.view">
<field name="name">Individual Form</field>
<field name="model">sale.individual</field>
<field name="arch" type="xml">
<form>
<field name="individual_description" />
</form>
</field>
Related
I have a parent model (Plant) which sends the active_id (plant_id) in the context as a default:
class Plant(models.Model):
_name = 'db.plant'
_description = 'db.plant'
name = fields.Char("Name", required=True)
...
Plant view with a many2one relation between them:
<odoo>
<data>
<record id="plant_list_view" model="ir.ui.view">
<field name="name">db.plant.view</field>
<field name="model">db.plant</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
...
</tree>
</field>
</record>
<record id="plant_form_view" model="ir.ui.view">
<field name="name">db.form.view</field>
<field name="model">db.plant</field>
<field name="arch" type="xml">
<form>
<sheet>
<group string="Plant">
<field name="name"/>
...
</group>
<group string="Components">
<notebook>
<page string="Data Sources">
<field name="data_source_ids" context="{'default_plant_id': active_id}">
<tree >
<field name="name"/>
...
<!-- <field name="plant_id"/> -->
</tree>
</field>
</page>
</notebook>
</group>
</sheet>
</form>
</field>
</record>
<record id="plant_action" model="ir.actions.act_window">
<field name="name">plant</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">db.plant</field>
<field name="view_mode">tree,form</field>
</record>
</data>
and his child model (Data_source):
class DataSource(models.Model):
_name = 'db.data_source'
_description = 'db.data_source'
name = fields.Char("Name", required=True)
plant_id = fields.Many2one('db.plant', name="Plant", required=True)
data_source view:
<odoo>
<data>
<record id="data_source_list_view" model="ir.ui.view">
<field name="name">db.data_source.view</field>
<field name="model">db.data_source</field>
<field name="arch" type="xml">
<tree>
<field name="name"/>
...
</tree>
</field>
</record>
<record id="data_source_form_view" model="ir.ui.view">
<field name="name">db.form.view</field>
<field name="model">db.data_source</field>
<field name="arch" type="xml">
<form>
<group>
<field name="name"/>
...
</group>
<group>
<field name="plant_id" domain="[('id', '=', context.default_plant_id)]" />
</group>
<group>
</group>
</form>
</field>
</record>
<record id="data_source_action" model="ir.actions.act_window">
<field name="name">Data Source</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">db.data_source</field>
<field name="view_mode">tree,form</field>
</record>
</data>
I know that the default_plant_id is received in the child because the plaint_id domain is filtering just my plant_id but I am not able to set it automatically as a default value. I need to click on the list and select my plant_id (which is the only element in the list).
I also tried adding an #api.on_change("plant_id") in the child (data_source) in two ways:
Option 1:
#api.onchange('plant_id')
def onchange_plant_id(self):
if self.plant_id:
context = dict(self._context or {})
return [(0,0,{'plant_id': context.get('default_plant_id')})]
giving me this exception:
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/albertocrespo/tools/odoo/odoo/http.py", line 640, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/Users/albertocrespo/tools/odoo/odoo/http.py", line 316, in _handle_exception
raise exception.with_traceback(None) from new_cause
AttributeError: 'list' object has no attribute 'get'
Option 2 (I am not sure what to send as a response, but the write statement is also not working):
#api.onchange('plant_id')
def onchange_your_many_to_one_field(self):
context = dict(self._context or {})
self.plant_id.write({'id':context.get('default_plant_id')})})
res = {}
return res
I need to auto-select my plant as an option in the data_source view before saving it.
There is no need of onchange method or anything on xml side - context level. You can simply use field plant_id in a relation one2many field data_source_ids.
Try with this:
data_source_ids = fields.One2many("db.data_source", "plant_id")
I am trying to inherit res.users model of odoo12 but getting an error.
My code is as follows:
model.py
class SaleIndividual(models.Model):
_name = 'sale.individual'
_inherit = 'res.users'
individual_description = fields.Char()
view.xml
<?xml version="1.0"?>
<odoo>
<record id="view_form_sale_custom_individual" model="ir.ui.view">
<field name="name">Individual Form</field>
<field name="model">sale.individual</field>
<field name="arch" type="xml">
<form string="Individual"> <group>
<field name="name" />
<field name="individual_description" />
<label for="login" class="oe_edit_only" string="Email Address"/>
<h2>
<field name="login"
placeholder="email#yourcompany.com"/>
</h2>
</group>
</form>
</field>
</record>
<record id="view_tree_sale_custom_individual" model="ir.ui.view">
<field name="name">Individual Form</field>
<field name="model">sale.individual</field>
<field name="arch" type="xml">
<tree>
<field name="individual_description" />
</tree>
</field>
</record>
duplicate key value violates unique constraint
"res_groups_users_rel_gid_uid_key"
DETAIL: Key (gid, uid)=(1, 1) already exists.
If you want to inherit res.user you need to write the code as below:
Use like below:
class SaleIndividual(models.Model):
_name = 'sale.individual'
_inherits = 'res.users'
name = fields.Char()
email = fields.Char()
individual_description = fields.Char()
So I'm trying to inherit hr.employee in odoo but there's an error that I don't know why since I'm doing it like in the tutorials. (Link to the tutorial)
Here's my code: in
employee.py
class Employee(models.Model):
""" Class Employee """
_inherit = 'hr.employee'
_description = 'A custom class of employee'
esia = fields.Float(digits=(12, 2))
cnaps = fields.Float(digits=(12, 2))
employees.xml
<record model="ir.ui.view" id="employee_form_view">
<field name="name">employee.form</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="arch" type="xml">
<notebook position="inside">
<page string="Sécurité Sociale">
<group>
<field name="cnaps"/>
<field name="esia"/>
</group>
</page>
</notebook>
</field>
</record>
<record model="ir.actions.act_window" id="employee_list_action">
<field name="name">Employees</field>
<field name="res_model">hr.employee</field>
<field name="view_mode">tree,form</field>
</record>
And what I get as error when trying to upgrade my module is:
Invalid model name 'hr.employee' in action definition
Thanks to #Amal, I defined hr module in _manifest_.py then it worked.
Added hr here: 'depends': ['base', 'hr'],
my problem is that when I use inheritance, records from parent class don't display
this is my class :
class LabTestCashRegister (models.Model):
_name = "medical.cash"
_inherit = "medical.lab.patient"
comment = fields.Text(store=True,size=2000000)
type_In = fields.Char(default='Reste', readonly=False)
and this is my view:
<record model="ir.ui.view" id="medical_lab_cash_tree_id">
<field name="name">cash</field>
<field name="model">medical.cash</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree>
<field name="patient_id"/>
<field name="cat"/>
<field name="test_type_id"/>
<field name="state_money"/>
<field name="Avance"/>
<field name="Amount"/>
<field name="Reste"/>
<field name="comment"/>
<field name="type_In"/>
</tree>
</field>
</record>
(Amount, Reste, Avance,state_money exist on the parent class)
Change your LabTestCashRegister to
class LabTestCashRegister (models.Model):
_inherit = "medical.lab.patient"
comment = fields.Text(store=True,size=2000000)
type_In = fields.Char(default='Reste', readonly=False)
This should work for you. Let me know if not.
I want to few custom field in mrp.bom table in order to calculate the real row material consumption starting from drawing dimensions.
here is my .py code
from osv import osv, fields
class mrp_bom(osv.osv):
_inerhit = 'mrp.bom'
#_name = 'mrp.bom'
_columns = {
'Residuo_barra': fields.float(string='Residuo Barra', required=False),
'Sfrido': fields.float(string='Sfrido mm', required=False),
'L_barra': fields.float(string='lunghezza barra mm', required=False),
'L_pezzo_a_disegno': fields.float(string='L a disegno in mm', required=False),
'L_pezzo_calcolata': fields.float(string='Lunghezza calcolata', required=False),
}
_defaults = {
'Residuo_barra': 300.0,
'Sfrido': 4.0,
'L_barra': 3000.0,
}
def button_Calcola(self, cr, uid, ids, L_pezzo_a_disegno, Residuo_barra, Sfrido, L_barra, conext=None):
#calcola il consumo effettivo della barra
barra_utile = L_barra - Residuo_barra
numero_pezzi = int(barra_utile / (L_pezzo_a_disegno + Sfrido))
res = {
'L_pezzo_calcolata': (L_barra / numero_pezzi)
}
return {'value': res}
mrp_bom()
and here is the .xml
<record id="mrp_bom_tree_view" model="ir.ui.view">
<field name="name">mrp.bom.tree</field>
<field name="model">mrp.bom</field>
<field name="inherit_id" ref="mrp.mrp_bom_tree_view"/>
<field name="arch" type="xml">
<field name="product_id" position="after">
<field name="L_pezzo_calcolata" />
</field>
</field>
</record>
<record id="mrp_bom_component_tree_view" model="ir.ui.view">
<field name="name">mrp.bom.component.tree</field>
<field name="model">mrp.bom</field>
<field name="inherit_id" ref="mrp.mrp_bom_component_tree_view"/>
<field name="arch" type="xml">
<field name="product_id" position="after">
<field name="L_pezzo_calcolata" />
</field>
</field>
</record>
<record id="mrp_bom_form_view" model="ir.ui.view">
<field name="name">mrp.bom.form</field>
<field name="model">mrp.bom</field>
<field name="inherit_id" ref="mrp.mrp_bom_form_view"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='bom_lines']/tree" position="inside" >
<field name="Residuo_barra" />
<field name="Sfrido" />
<field name="L_barra" />
<field name="L_pezzo_a_disegno" />
<field name="L_pezzo_calcolata" />
</xpath>
</field>
</record>
</data>
during the installation process I get :
ValidateError
Error occurred while validating the field(s) arch: Invalid XML for View Architecture!
If I check into the module structure Setting\Database structure\models\mrp_bom
the field have been added, But If I menage view in bom view the fields are not available!
Change the record id of inherited views so they are not the same.
For example:
<record id="mrp_bom_tree_view_add_field_L_pezzo_calcolata" model="ir.ui.view">
<field name="name">mrp_bom_tree_view_add_field_L_pezzo_calcolata</field>
<field name="model">mrp.bom</field>
<field name="inherit_id" ref="mrp.mrp_bom_tree_view"/>
<field name="arch" type="xml">
<field name="product_id" position="after">
<field name="L_pezzo_calcolata" />
</field>
</field>
</record>