Set default value (auto select a line in the child view) from a list for a Many2One value in Odoo 14 - odoo

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")

Related

Getting mixing apples and oranges error while inheriting res.users

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>

2 fields in tree view

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>

How send default value for field from view to other view

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.

How to get the field value and assign to the variable in python

I am using openerp 6.My form contains one text box and I need to get that value and assign it to a variable so to perform calculations and to return a result to store in a new textbox...
I have give .py file here. This will calculate square of the number
from osv import osv
from osv import fields
class test_base(osv.osv):
_name='test.base'
_columns={
'first':fields.integer('Enter Number here'),
'result':fields.integer('Display calclation result'),
}
def first_change(self, cr, uid, ids,first,context=None):
r=first*first
return {'value':{'result':r}}
test_base()
xml file is given here
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record model="ir.ui.view" id="test_base_form">
<field name="name">test.base.form</field>
<field name="model">test.base</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="best Base">
<field name="first" on_change="first_change(first)"/>
<field name="result"/>
</form>
</field>
</record>
<record model="ir.ui.view" id="test_base_tree">
<field name="name">test.base.tree</field>
<field name="model">test.base</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="Test Base">
<field name="first"/>
<field name="result"/>
</tree>
</field>
</record>
<record model="ir.actions.act_window" id="action_test_seq">
<field name="name">Test Base</field>
<field name="res_model">test.base</field>
<field name="view_type">form</field>
<field name="view_mode">form,tree</field>
</record>
<menuitem id="menu_test_base_main" name="Test Base">
</menuitem>
<menuitem id="menu_test_base_sub" parent="menu_test_base_main" name="Square number" action="action_test_seq">
</menuitem>
</data>
</openerp>

Openerp 6.1 put product category in sale order line

I want to show the product category in the sale.order.line.tree view of a sales order with the following code which I wrote. It shows the category button under the group by button but on clicking it, I get the following error and I have don't know how to solve the bug:assert groupby_def and groupby_def._classic_write, "Fields in 'groupby' must be regular database-persisted fields (no function or related fields), or function fields with store=True"
AssertionError: Fields in 'groupby' must be regular database-persisted fields (no function or related fields), or function fields with store=True
Here is my code:
from osv import fields, osv<code>
class sales_order_line_category(osv.osv):
_name='sale.order.line'
_inherit='sale.order.line'
_columns={'categ_id': fields.related('product_id', 'categ_id', type='many2one', relation='product.categ_id'),
}
sales_order_line_category()
My view:
`<?xml version="1.0" encoding="utf-8"?>
<record id="view_sale_orderlinecategory" model="ir.ui.view">
<field name="name">sale.order.line.categoryinherit</field>
<field name="model">sale.order.line</field>
<field name="type">tree</field>
<field name="inherit_id" ref="sale.view_order_line_tree"/>
<field name="arch" type="xml">
<field name="name" position="after">
<field name="categ_id" string="Category"/>
</field>
</field>
</record>
<record id="view_sale_orderlinecategory2" model="ir.ui.view">
<field name="name">sale.order.line.categoryinherit2</field>
<field name="model">sale.order.line</field>
<field name="type">search</field>
<field name="inherit_id" ref="sale.view_sales_order_uninvoiced_line_filter"/>
<field name="arch" type="xml">
<group expand="0" string="Group By..." >
<filter string="Category of Product" icon="terp-stock_symbol-selection" name="Category" context="{'group_by':'categ_id'}"/>
</group>
</field>
</record>
<record id="view_sale_orderlinecategory3" model="ir.ui.view">
<field name="name">sale.order.line.categoryinherit3</field>
<field name="model">sale.order,line</field>
<field name="type">search</field>
<field name="inherit_id" ref="sale.view_sales_order_uninvoiced_line_filter"/>
<field name="arch" type="xml">
<field name="name" position="before">
<field name="categ_id" string="Category"/>
</field>
</field>
</record>
</data>
`
WOuld you please try with following code replace?
'categ_id': fields.related('product_id', 'categ_id', type='many2one', relation='product.category', store=True),
Check that your field categ_id must be appear in tree view of sale.order.line where you try to do group by categ_id
Hope this help