Save value in Transient model Odoo 10 - odoo

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.

Related

Odoo 9 Is there a way to handle authorization with different groups on a certain field in form view?

I'm trying to create a form view.
<field name="is_positive" attrs="{'readonly':[('state','==','final')]}"/>
However there is many attributes like groups and invisible related to authorization so that certain group of people can see the field.
groups="base.group_hr_user"
But Is there a way for certain group can edit the field and the other group cannot?
add a new field to check whether the user is manager or user.
New Api Method
check_user = fields.Boolean(string='user',compute='_compute_user_check')
#api.multi
def _compute_user_check(self):
if self.user_has_groups('purchase.group_purchase_manager'):
self.check_user =True
In view
<field name="is_positive" attrs="{'readonly':[('check_user','=','True')]}"/>
First of all, you cannot use a domain like this one
<field name="is_positive" attrs="{'readonly':[('state','==','final')]}"/>
There is not a '==' operator, use = instead.
Now, to answer your question, if you want to create a special view for another group in which some elements are readonly for one group, and editable in the other, you have to it this way.
For the default view :
<record id="some_model_view" model="ir.ui.view">
<field name="name">some.model.form</field>
<field name="model">some.model</field>
<field name="arch" type="xml">
<form>
<field name="some_field" readonly="1"/>
</form>
<field/>
</record>
For a certain group :
<record id="some_model_view_for_other_group" model="ir.ui.view">
<field name="name">some.model.form</field>
<field name="model">some.model</field>
<field name="inherit_id" ref="my_module.some_model_view"
<field name="groups_id" eval="[(6, 0, [ref('some.first_group')])]" />
<field name="arch" type="xml">
<field name="some_field" position="attributes">
<attribute name="readonly">0</attribute>
</field>
<field/>
</record>
I will show one example to how this functionality works in sale group.
I make the unit price field in the sale order line makes readonly we select the user group user:own documents only The field is editable for other 2 groups user:All documets and manager
Firstly I create a boolean field for checking the user belongs to which group
is_own_user = fields.Boolean(string="Own user", compute='compute_own_user')
Then assigns the boolean field is True when the user belongs the group user:own documents only otherwise assigns to False
#api.depends('product_id')
def compute_own_user(self):
res_user_id = self.env['res.users'].search([('id', '=', self._uid)])
for rec in self:
if res_user_id.has_group('sales_team.group_sale_salesman') and not res_user_id.has_group('sales_team.group_sale_salesman_all_leads'):
rec.is_own_user = True
else:
rec.is_own_user = False
in xml make is_own_user invisible and replaces the unit price field
<xpath expr="//notebook/page/field[#name='order_line']/tree/field[#name='price_unit']" position="replace">
<field name="price_unit" attrs="{'readonly': [('isown_user', '=', True)]}" />
</xpath>

Odoo 9 How to sort order for a field in form view

I've been trying to modify project issue object.
and When I click Assign to field, It shows a list of users with dropdown.
But I'd like to change its order to DESC.
Is there any thing I can do in View?
here is my code below
<record id="project_issue_custom_form" model="ir.ui.view">
<field name="inherit_id" ref="project_issue.project_issue_form_view"/>
<field name="model">project.issue</field>
<field name="arch" type="xml">
<field name="user_id" position="attributes">
<attribute name="default_order">sequence desc</attribute>
</field>
</field>
</record>
Also I tried in controller
class Project_issue(models.Model):
_inherit = "project.issue"
_order = "user_id desc"
But It still doesn't affected.
The tag default_order can only be used for lists and kanban views. But you want to change the order of the content of a many2one field (user_id on project.issue).
Your second approach has potential. But it is the wrong model:
class ResUsers(models.Model):
_inherit = "res.users"
_order = "name desc" # or id

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

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>

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