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()
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 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>
In the product category, I added a product_ids field and I want to display it with a tree view showing name and default_code of products. For some reason I get the error "Field default_code does not exist"
<record id="view_product_category_qty_discount" model="ir.ui.view">
<field name="name">product.category.inherit.qty.discount.Config Hetlita</field>
<field name="model">product.category</field>
<field name="type">form</field>
<field name="inherit_id" ref="product.product_category_form_view" />
<field name="arch" type="xml">
<form position="inside">
<group col="2" colspan="2">
<separator string="Quantity for discount" colspan="2"/>
<field name="qty_for_discount" />
</group>
<group>
<field name="product_ids" widget="many2many_tags"/>
<tree>
<field name="name"/>
<field name="default_code"/>
</tree>
</group>
</form>
</field>
</record>
class ProductCategory(models.Model):
_inherit = 'product.category'
qty_for_discount = fields.Float(string='Qty For Discount')
product_ids = fields.Many2many(
'product.template', string='Products')
That's because there is no default_code on model product.template but instead on its variants with model product.product. I would change the field on product.category to a One2Many on product.product:
product_ids = fields.One2many(
comodel_name='product.product',
inverse_name='categ_id',
string='Products')
And there is a mistake in your xml:
<group>
<field name="product_ids">
<tree>
<field name="name"/>
<field name="default_code"/>
</tree>
</field>
</group>
I developed a Location Module in Odoo where I can add country's & states related to that Country. Then Iam calling this module in another Module Where I have to do like:- when selecting a country, it should automatically populate the states belong to that country in the state field.How this can be done? I tried Onchange function,But didnt worked? I will Provide my model & view code here below.
from openerp import models, fields, api
class peniel(models.Model):
_name='peniel'
name = fields.Char(string="Name")
product = fields.Many2one('product.template', string='Product', required=True)
employee = fields.Many2one('hr.employee', string='Employee', required=True)
customer = fields.Many2one('res.partner', string='Customer', required=True)
country = fields.Many2one('location', string='Country', required=True)
state = fields.Many2one('state', string='State', required=True)
date_d = fields.Date(string="Date Activity")
comment = fields.Text(string="Comments")
#api.onchange('country')
def onchange_country(self):
if self.country:
self.state = self.country.state
View File will be like:
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="action_peniel" model="ir.actions.act_window">
<field name="name">Peniel </field>
<field name="res_model">peniel</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="search_view_id" eval="False"/>
<field name="context">{}</field>
<field name="help">Create new Record</field>
</record>
<menuitem name="Peniel" id="peniel_roof" sequence="60"/>
<menuitem id="menu_peniel_roof" name="Peniel" parent="peniel_roof" sequence="1"/>
<menuitem action="action_peniel" id="menu_action_peniel" parent="menu_peniel_roof" sequence="20"/>
<record id="view_location_tree" model="ir.ui.view">
<field name="name">peniel.tree</field>
<field name="model">peniel</field>
<field name="arch" type="xml">
<tree string="Peniel">
<field name="name"/>
<field name="product"/>
<field name="employee"/>
<field name="customer"/>
<field name="country"/>
<field name="state"/>
<field name="date_d"/>
<field name="comment"/>
</tree>
</field>
</record>
<record id="view_location_form" model="ir.ui.view">
<field name="name">peniel.form</field>
<field name="model">peniel</field>
<field name="arch" type="xml">
<form string="Peniel">
<group>
<field name="name"/>
<field name="product"/>
<field name="employee"/>
<field name="customer"/>
<field name="country"/>
<field name="state"/>
<field name="date_d"/>
<field name="comment"/>
</group>
</form>
</field>
</record>
</data>
</openerp>
Their is no require of onchange function. We may handle with below tricks.
Replace your field definition
country = fields.Many2one('location', string='Country', required=True)
state = fields.Many2one('state', string='State', required=True)
with
country = fields.Many2one('res.country', string='Country', required=True)
state = fields.Many2one('res.country.state', string='State', required=True)
Now change field on .xml side:
Replace field
<field name="state"/>
with
<field name="state" domain="[('country_id', '=', country)]"/>
I need to create one2one relation in openerp7. I read many articles about this idea and I could to type the following code
problem is : that openerp7 does not send value from parent view (calculation) to child (container)
this my code
testproject.py:
from osv import fields,osv
class container(osv.osv):
_name='container'
_columns={
'calculation_id': fields.many2one('calculation','Calculation'),
'name': fields.char('Name', size=32),
}
container()
class calculation(osv.osv):
_name='calculation'
_columns={
'container_id': fields.many2one('container','Container'),
'namefull': fields.char('Name Full', size=32),
}
calculation()
xml code:
<?xml version="1.0"?>
<openerp>
<data>
<record model="ir.ui.view" id="view_container_form">
<field name="name">container.form</field>
<field name="model">container</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Container">
<field name="name" select="1"/>
<field name="calculation_id" context="{'default_container_id': active_id}" />
</form>
</field>
</record>
<record model="ir.actions.act_window" id="action_container">
<field name="name">Container</field>
<field name="res_model">container</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="Container/Container" id="menu_container"/>-->
<menuitem name="Container" id="menu_container_item" parent="menu_container" action="action_container"/>
<record model="ir.ui.view" id="view_calculation_form">
<field name="name">calculation.form</field>
<field name="model">calculation</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Calculation">
<field name="namefull" />
<field name="container_id" />
</form>
</field>
</record>
<record model="ir.actions.act_window" id="action_calculation">
<field name="name">Calculation</field>
<field name="res_model">calculation</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="Calculation" id="menu_calculation_item" parent="menu_container" action="action_calculation"/>
</data>
</openerp>
You are correct, OpenERP will not move data around for you -- you will need to modify your Python code to do it.
Oh, and you should name your tables with your model as well -- I'll use a fake model name of my_model:
class calculation(osv.Model):
_name = 'my_model.calculation'
_columns = {
'container_id' fields.many2one('my_model.container', 'Container'),
'namefull': fields.char('Name Full', size=32),
}
def create(self, cr, uid, values, context=None):
new_id = super(calculation, self).create(cr, uid, values, context=context)
self.pool.get('my_model.container').create(cr, uid, {'calculation_id':new_id, 'name':values['namefull'])
return new_id
And something similer to write() in case namefull is updated.