Making a module inherit from sale module - module

i created a module to inherit from the Sale module , the field i changed appers just fine on the interface here is my module :
ventes.py
from osv import fields,osv
import time
from datetime import datetime
from tools.translate import _
class ventes(osv.osv):
_inherit='sale.order'
_columns = {
'prenom': fields.many2one('patient','Patient', required=True),
}
ventes()
ventes_view.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record model="ir.ui.view" id="view_sales_inherit">
<field name="name">sale.order.form.inherit</field>
<field name="model">sale.order</field>
<field name="type">form</field>
<field name="inherit_id" ref="sale.view_order_form">form</field>
<field name="arch" type="xml">
<field name="name" position="after">
<label for="prenom" class="oe_edit_only"/>
<h1>
<field name="prenom"/>
</h1>
</field>
<field name="partner_id" position="replace"/>
</field>
</record>
</data>
</openerp>
the problem is when i create a sale order , in the place of the client names i get the patients name ( witch is what i was looking for ) but the problem is when i want to add a product to that sale order i get the following error:
Error: Could not get field with name 'parent.partner_id' for onchange 'product_uom_change(parent.pricelist_id,product_id,product_uom_qty,product_uom,product_uos_qty,product_uos,name,parent.partner_id, False, False, parent.date_order, context)'
Plz help me, i dont know how to make it work with my patient's id and not the partner_id ??!!

what are you doing it totally nonsense? what are doing is you removed the partner_id, but in the onchange method of the sale_order it used as like in your case product_uom_onchane()
in this onchange method partner_id is passed from xml side, so if you want to used pateint_id instead of partner_id, then you have to change all over whre partner_id used to patient_id
hope this helpful

Related

Inherit Abstract model and add new field

I need to inherit mail.group kanban view (mail.view_group_kanban). But some of the fields in this kanban view is defined in mail.thread model. Now my requirement is, i need to display count of members in a group on kanban view.To do this i have inherited mail.thread model and added new field. But i getting an error:Fieldnew_fielddoes not exist.
I have tried below code:
*.py
from openerp import models, fields, api, _
class mail_thread(models.Model):
_inherit = 'mail.thread'
_columns={
'new_field': fields.char(string='New Field')
}
*.xml
<record id="view_group_kanban_inherit" model="ir.ui.view">
<field name="name">view.group.kanban.inherit</field>
<field name="model">mail.group</field>
<field name="inherit_id" ref="mail.view_group_kanban"/>
<field name="arch" type="xml">
<field name="alias_domain" position="after" >
<field name="new_field" />
</field>
<xpath expr="//div[#class='oe_kanban_footer_left']" position="after">
<field name="new_field" />
</xpath>
</field>
</record>
You should define class definition like below:
class MailThread(models.AbstractModel):
_inherit = "mail.thread"
Best Thanks,
Ankit H Gandhi.
just code like this :
from openerp import models, fields, api, _
class MailThread(models.AbstractModel):
_inherit = 'mail.thread'
_columns={
'new_field': fields.char(string='New Field')
}
but if you want to override fields in AbstractModel ,I dont know for it .
thanks

Error while adding custom field using module: ParseError: "arch" while parsing file

I am trying to add a custom field to Odoo 9 res.partner model using module. I have used scoffold command to generate the module files and added following code to models.py and views.xml.
models/models.py
from openerp import models, fields, api
class SeicoPartner(models.Model):
_name = 'res.partner'
_inherit = 'res.partner'
no_of_ac = fields.Integer('No of AC', default=0)
review = fields.Char('Company Review')
views/views.xml
<openerp>
<data>
<record id="res_partner_field_ac" model="ir.ui.view">
<field name="no_of_ac">10</field>
</record>
</data>
</openerp>
Upon installation of this module from Apps screen, I got the following error:
Traceback (most recent call last):
...
File "C:\Program Files (x86)\Odoo 9.0-20160719\server\openerp\addons\base\ir\ir_ui_view.py", line 344, in create
ParseError: "arch" while parsing file:///C:/Program%20Files%20(x86)/Odoo%209.0-20160719/server/openerp/addons/mymodule1/views/views.xml:4, near
<record id="res_partner_field_ac" model="ir.ui.view">
<field name="no_of_ac">10</field>
</record>
From the Settings -> Database Structure -> Fields I can see that res.partner has the no_of_ac field, but the field is not visible while editing any customer details.
That's because you're missing the arch field with describes the type of view (either xml or html), in most cases xml is just fine,
You're also missing the model name, the view name, so odoo doesn't know which model your view belongs to. you also have to specify the existing model form you want to over-ride and the position where you want the new field to be, in this case i used an xpath expression to display the field after the website field in the parent view, it can be anywhere you want it to be.
<openerp>
<data>
<record id="res_partner_field_ac" model="ir.ui.view">
<field name="name">res.partner.form</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='website']" position="after">
<field name="no_of_ac" />
</xpath>
</field>
</record>
</data>
</openerp>
also you don't need to specify _name if you just want to extend a model and add extra fields to it, so change your model code to this
from openerp import models, fields, api
class SeicoPartner(models.Model):
_inherit = 'res.partner'
no_of_ac = fields.Integer('No of AC', default=0)
review = fields.Char('Company Review')
If you want to add a data record you should use res.partner as model and define the required fields:
<record id="res_partner_field_ac" model="res.partner">
<field name="no_of_ac">10</field>
<field name="name">NEW PARTNER NAME</field>
<!-- define required fields -->
</record>
To define a view, take a look at Odoo views
You are inserting a new record in ir.ui.view data model.
<openerp>
<data>
<record id="res_partner_field_ac" model="ir.ui.view">
<field name="no_of_ac">10</field>
</record>
</data>
</openerp>
But you want to insert data to : res.partner
<openerp>
<data>
<record id="res_partner_field_ac" model="res.partner">
<field name="name">a name here because it's required when the field type != 'contact' </field>
<field name="no_of_ac">10</field>
</record>
</data>
</openerp>
NOTE :you encountered a problem because arch is a requrired field inir.ui.view model.

how can i use onchange from product field to one2many field of purchase order line odoo

Like i have one field in product.template with the field name squ_meter which i need to copy this value in custom field of purchase order line with same field name i.e squ_meter and i want to apply onchange on purchase order line field
Any kind of help would be much appreciated. Thanks in advance
Here's my code
class purchase_order_line(osv.osv):
_inherit = 'purchase.order.line'
_columns ={'squ_meter' : fields.float('Square Meter'),
}
purchase_order_line.xml
<field name="name">purchase.order.inherit</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<xpath expr="//page[#string='Products']//field[#name='order_line']//field[#name='product_qty']" position="after">
<field name="squ_meter"/>
</xpath>
</field>
Related Fields
Related field is useful when you want to keep the values of any relational fields except it's reference then it would be easier way to do it.
OLD API
_columns ={
'squ_meter': fields.related('product_id','squ_meter', type='float', relation='product.product', string='Square Meter', readonly=True),
}
Where:
The first set of parameters are the chain of reference fields to
follow, with the desired field at the end.
type is the type of that desired field.
Use relation if the desired field is still some kind of reference. relation is the table to look up that reference in.
NEW API
There is not anymore fields.related fields.
Instead you just set the name argument related to your model:
squ_meter = Fields.Float(string='Square Meter', related='product_id.squ_meter' , readonly=True)
purchase_order_line.xml
<field name="name">purchase.order.inherit</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<xpath expr="//page[#string='Products']//field[#name='order_line']//field[#name='product_qty']" position="after">
<field name="squ_meter" readonly="1" />
</xpath>
</field>

Odoo use xml data to update products

Hi I have to update a product data with a new module that add some features to product.product model but when I try to install the new module data it give me a error because its trying to create a new product and not to update the existing product
example: product record
<record model="product.product" id="product_product_11109613400">
<field name="list_price">0.07388597</field>
<field name="standard_price">0.0739</field>
<field name="default_code">11109613400</field>
<field name="name">LAMP</field>
<field name="categ_id" ref="product_category_categoria_06"/>
<field name="uom_po_id" ref="product.product_uom_unit"/>
<field name="uom_id" ref="product.product_uom_unit"/>
<field name="sale_ok" eval="False"/>
<field name="purchase_ok" eval="True"/>
</record>
the update xml:
<record model="product.product" id="product_product_11109613400">
<field name="hazard_ids" eval="[(4, ref('product_safety_advice_r4'))]"/>
</record>
I would look in ir_model_data and see what the name field is set to for the original create and that might give you a clue as to why the update is failing. If that doesn't work you could use a function that runs when the module is installed/updated.
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<function model="product.product" name="do_foo" />
</data>
</openerp>
and in product.product override
#api.model
def do_foo(self):
...

How do I get the value from a form field using Odoo?

I have this field in a form view:
<field name="value"/>
I want to get the value from the field when someone wants to add a new value, the way that I might do $_GET['value'] in PHP.
Simple example:
I want, when the user inserts a value, for the program to check if it is greater than sum of all values and print an error message like:
Cannot add the value because it is greater than the sum of all values
I've written this so far:
view.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="action_test" model="ir.actions.act_window">
<field name="name">Insert</field>
<field name="res_model">test.odoo</field>
<field name="view_mode">form,tree</field>
</record>
<record id="view_tree_test" model="ir.ui.view">
<field name="name">Inset.tree</field>
<field name="model">test.odoo</field>
<field name="arch" type="xml">
<tree string="T">
<field name="create_uid"/>
<field name="value" sum="Total Sum"/>
</tree>
</field>
</record>
<record id="view_from_test" model="ir.ui.view">
<field name="name">Inset.form</field>
<field name="model">test.odoo</field>
<field name="arch" type="xml">
<form string="T_Form">
<group>
<field name="value"/>
</group>
</form>
</field>
</record>
<menuitem name="Test Module" id="main_menu_test" sequence="3"/>
<menuitem name="TEST" id="sub_menu" parent="main_menu_test"/>
<menuitem action="action_test" id="action_menu" parent="sub_menu" />
</data>
</openerp>
test.py
from openerp import models
from openerp import fields
class test(models.Model):
_name = "test.odoo"
value = fields.Integer()
You cannot think about form view in odoo in isolation from the database layer.
The data from the form is immediately (and automatically) saved in the database. You can later access it using the ORM methods.
I don't know what exactly you want to achieve, so it's hard for me to give you a concrete example. Every form view is associated with a single ORM model. If you want to do do something with the data immediately before/after it is saved, you would usually subclass the ORM model and overwrite one of its methods.
class Foo(models.Model):
_inherit = 'other.foo'
#api.model
def create(self, vals):
record = super(Foo, self).create(vals)
print "A new Foo with name={} and bar={} has been created!".format(
record.name,
record.bar,
)
return record
This is how you validate a form:
from openerp import models, fields, api, exceptions
class test(models.Model):
_name = 'test.odoo'
value = fields.Integer()
#api.one
#api.constrains('value')
def _check_value(self):
if self.value > 25:
raise exceptions.ValidationError("The value is too large!")