Inherit Abstract model and add new field - odoo

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

Related

Save value in Transient model Odoo 10

I added a new field in account.config.settings model. It displays the new field in settings page and can enter the value. But when i reopen the page the value is not there. I know the Transient model won't store values for long.
But rest of the values still there, how can i achieve this?
Below is my code.
*.py
class AccountSettings(models.TransientModel):
_inherit='account.config.settings'
#api.one
def _get_header(self):
header = fields.Char('Header')
*.xml
<record id="view_account_config_settings_inherit" model="ir.ui.view">
<field name="name">view.account.config.settings.inherit.form</field>
<field name="model">account.config.settings</field>
<field name="inherit_id" ref="account.view_account_config_settings"/>
<field name="arch" type="xml">
<xpath expr="//group[#name='accounting']" position="after">
<group string="Reports" name="reports">
<field name="header" class="oe_inline"/>
</group>
</xpath>
</field>
</record>
In account.config.settings Model you can save your value by using this :
class AccountSettings(models.TransientModel):
_inherit='account.config.settings'
header = fields.Char('Header')
#api.multi
def set_header_defaults(self):
return self.env['ir.values'].sudo().set_default(
'account.config.settings', 'header', self.header)
Try this code:
class AccountSettings(models.TransientModel):
_inherit='account.config.settings'
#api.one
def _get_header(self):
header = fields.Char('Header',config_parameter='header.configuration')
you can name the attribute config_parameter anything you want. And it will be used to get the value of header from other models.
example:
test = self.env['ir.config_parameter'].get_param('**header.configuration**', '').strip()
test will return the temporary stored value in account.config.settings.

How do I add multiple models to one view?

I'm trying to use three different models in one view. I've created a new model that inherits the models which seems to work fine.
from openerp import models, fields, api
class ProjectNote(models.Model):
_name = "triangle.project.note"
_inherit = ["note.note", "project.project", "triangle.note"]
My problem is in the view. I use my new model as the model and inherit a view from project.
<record id="view_project_notes_form" model="ir.ui.view">
<field name="name">triangle.project.note.form</field>
<field name="model">triangle.project.note</field>
<field name="inherit_id" ref="project.edit_project"/>
<field name="arch" type="xml">
<data>
<xpath expr="//field[#name='privacy_visibility']" position="replace">
<h2>
<field name="title" placeholder="Title"/>
</h2>
</xpath>
</data>
</field>
</record>
I don't get any errors but my field is not being added.
Any help is appreciated!
If you're trying to open a project.project view and wondering why there is no field title in it: there can't be. You aren't extending the project view for model project.project but are defining a form view for your model triangle.project.note which inherits the project view.
So the project views are untouched, you've just created the first form view for your new model.

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

Making a module inherit from sale 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