I'm trying to implement a many2one selection field where you select a project from. If you've selected a project then there's another many2one field where you can select the task from. These task all need to be from the selected project.
Currently I got this (note that I couldn't test it because I kept getting an XML error):
class purchase_order(osv.osv):
_inherit = 'purchase.order'
def get_task(self, cr, uid, ids, project_id, context=None):
task_obj = self.pool.get('project.task')
for task in task_obj.browse(cr, uid, ids, context):
task_ids = task_obj.search(cr, uid, [(task.project_id.id, '=', project_id)])
ids_cus = []
for cus in task_obj.browse(cr, uid, task_ids, context):
if cus.project.id.id not in ids_cus:
ids_cus.append(cus.project_id.id)
self.write(cr, uid, ids, {'state_readonly': 'listed', 'task_ids': [(6, 0, ids_cus)]})
return True
_columns = {
'project_id': fields.many2one('project.project', 'Project'),
'task_id': fields.selection(get_task, 'Select task'),
}
And my XML looks like this:
<record id="purchase_order_form" model="ir.ui.view">
<field name="name">purchase.order.form</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<field name="origin" position="after">
<field name="project_id" on_change="_get_task(project_id)"/>
<field name="task_id" selection="widget"/>
</field>
</field>
</record>
What am I doing wrong with the XML here? And might there be another way?
No need to write method to filter tasks according to project in your case just change few things as follow.
class purchase_order(osv.osv):
_inherit = 'purchase.order'
_columns = {
'project_id': fields.many2one('project.project', 'Project'),
'task_id': fields.many2one('project.task', 'Tasks'),
}
and change your xml,
<record id="purchase_order_form" model="ir.ui.view">
<field name="name">purchase.order.form</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<field name="origin" position="after">
<field name="project_id" />
<field name="task_id" domain="[('project_id','=',project_id.id)]" widget="selection" />
</field>
</field>
</record>
In your existing code you have made small mistake,
selection="widget" is not valid in xml you should write widget="selection"
This solved my problem, I've added a fields.related for chained fields:
_columns = {
'task_id': fields.related('project_id', 'tasks', type='many2one', relation='project.task', store=True,
string='Task')
}
And then adjust my XML (Like #Empiro Technologies said):
<record id="purchase_order_form" model="ir.ui.view">
<field name="name">purchase.order.form</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<field name="origin" position="after">
<field name="project_id"/>
<field name="task_id" domain="[('project_id','=', project_id)]"/>
</field>
</field>
</record>
Related
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>
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>
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 can I show Category in stock move tree.
This is my coding.
My view file.
<record id="royalfood_stock_move_tree_view" model="ir.ui.view">
<field name="name">stock.move.tree</field>
<field name="model">stock.move</field>
<field name="inherit_id" ref="stock.view_move_tree" />
<field name="arch" type="xml">
<xpath expr="//field[#name='product_id']" position="before">
<field name="categ_id" groups="base.group_user"/>
</xpath>
</field>
</record>
My .py File
class custom_stock_move_tree(osv.osv_memory):
_columns = {
'categ_id': fields.related('product_id', 'categ_id', type='many2one' ,relation='product.category', store=True),
}
custom_stock_move_tree()
Any Help is Appreciated.
try this files.
your .py file
class custom_stock_move_tree(osv.Model): #their is different b/w osv.Model and osv.osv_memory before to use it read it's usage
_inherit = 'stock.move' # if you want to add data in existing module use inherit
_columns =
{
'categ_id': fields.related('product_id', 'categ_id', type='many2one' ,relation='product.category', store=True, string='Product Category'),
}
Your view.xml file
<record id="royalfood_stock_move_tree_view" model="ir.ui.view">
<field name="name">stock.move.tree</field>
<field name="model">stock.move</field>
<field name="inherit_id" ref="stock.view_move_tree" />
<field name="arch" type="xml">
<xpath expr="//field[#name='product_id']" position="before">
<field name="categ_id" groups="base.group_user"/>
</xpath>
</field>
After this see your Stock Move Tree view.