How to get the current (logged) user in XML Odoo V11? - odoo-11

I'm working on Employee Directory module in odoo11 and I want to set a notebook page invisible to the current user (Logged user) if he is different to the related user.
I tried to use user.id in XML but it does not work.
Here is my code :
<page name="hr_settings" string="HR Settings" attrs="{'invisible':[('user_id', '!=', user.id)]}">
<group>
<group string='Status' name="active_group">
<field name="company_id"/>
<field name="user_id" string="Related User"/>
</group>
</group>
</page>
The error message :
<class 'NameError'>: "name 'user' is not defined" while evaluating
"{'invisible': [('user_id', '!=', user.id)]}"
None" while parsing /opt/odoo/odoo/my_addons/hr_dz/views/employee_views.xml:5, near
<record id="view_employee_form" model="ir.ui.view">
<field name="name">hr.employee.form</field>
Any idea about that, please ?

One of the thing i know to use a field in attrs the field must be Mentionsed in the form. i don't know how to get the value of the user id in the form. but if there is not a short way like uid or user you can work arround this, just create a m2o field to res.users make this field compute field with store = False.
Please try this it useful to you.:
# by default store = False this means the value of this field
# is always computed.
current_user = fields.Many2one('res.users', compute='_get_current_user')
#api.depends()
def _get_current_user(self):
for rec in self:
rec.current_user = self.env.user
# i think this work too so you don't have to loop
self.update({'current_user' : self.env.user.id})
and you can use this field in your form.
<page name="hr_settings" string="HR Settings" attrs="{'invisible':[('user_id', '=', current_user)]}">
<group>
<group string='Status' name="active_group">
<field name="company_id"/>
<field name="user_id" string="Related User"/>
</group>
</group>
</page>

In your case basically, we can achieve directly with uid global expression variable on view level.
uid is also used into the expression evaluation in odoo xml view file
<page name="hr_settings" string="HR Settings" attrs="{'invisible':[('user_id', '!=', uid)]}">
<group>
<group string='Status' name="active_group">
<field name="company_id"/>
<field name="user_id" string="Related User"/>
</group>
</group>
</page>
No need to add and create any compute field for the code.
Please see the following view under the addons of Odoo V11
addons/project/project_view.xml.

Related

How to hide specific column in tree view for odoo12?

I have one field was the M2o and that opposite O2m. I try to display the O2m field in the M2o class just like that and display all the O2m field in the tree view. But I need to hide the one column is based on the ir.config_parameter
from odoo import models, fields, api
class AbcXyz(models.Model):
_name='abc.xyz'
b_ids=fields.One2many('xyz.abc','a_id',string="Xyz")
#api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
res = super(AbcXyz, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
if view_type=='form' and self.b_ids:
for line in self.b_ids:
is_applying_view = eval(self.env['ir.config_parameter'].sudo().get_param('test_module.is_applying_k_qty'))
doc = etree.XML(res['arch'])
if is_applying_view:
for node in doc.xpath("//field[#name='line.k_qty']"):
node.set('invisible', '0')
else:
for node in doc.xpath("//field[#name='line.cartoon_qty']"):
node.set('invisible', '1')
return res
class XyzAbc(models.Model)
_name='xyz.abc'
#api.multi
def get_default_k_qty_visible(self):
return eval(self.env['ir.config_parameter'].sudo().get_param('test_module.is_applying_k_qty'))
a_id=fields.Many2one('abc.xyz',string="ABC")
<!---other fields--->
k_qty=fields.Integer(string="Cartoon Qty", default=0)
is_k_qty_visible = fields.Boolean(string="Is K Qty Visible", compute=get_default_cartoon_qty_visible, store=True)
based is_k_qty_visible I try to hide the k_qty column.
<record id="abc_form_view" model="ir.ui.view">
<field name="name">abc.form.view</field>
<field name="model">abc.xyz</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="ABC Process">
<notebook>
<page string="K Qty">
<field name="b_ids" nolabel='1' mode="tree">
<tree>
<field name="k_qty" attrs="{'invisible':[('is_k_qty_visible','!=',True)]}" />
</tree>
</field>
</page>
</notebook>
</form>
</field>
<record>
But it not working for me.
This is not the answer of this question. I have share some information.
This code is used to hide fields in one2many(tree) in odoo11
<
field name="my_field" attrs="{'column_invisible': [('parent.field_name','=',False)]}" />
this type of code only works gives 'parent' in condition
I think this will type of code will work in odoo12 too.

context doesn't update value of type

I have these 2 buttons in form view:
<button name="%(action_view_task_make_situation)d" string="Create work situation" type="action" states="open" context="{'type': 'situation'}"/>
<button name="%(action_make_general_final_count)d" string="Create Final General Count" type="action" states="done" context="{'type': 'final_count'}"/>
with these actions:
<record id="action_view_task_make_situation" model="ir.actions.act_window">
<field name="name">Make Situation</field>
<field name="res_model">task.make.situation</field>
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
<field name="context">{'type':True}</field>
</record>
<record id="action_make_general_final_count" model="ir.actions.act_window">
<field name="name">Make General Final Count</field>
<field name="res_model">task.make.situation</field>
<field name="type">ir.actions.act_window</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
<field name="context">{'type':False}</field>
</record>
now I have task.make.situation model:
class TaskMakeSituation(models.TransientModel):
_name = "task.make.situation"
type = fields.Char(compute = "compute_type", string="Type", readonly= True)
#api.multi
def compute_type(self):
if self._context.get('type', True):
return "situation"
else:
return "final_count"
But when I click one of the buttons the wizard appears with an empty type field.
Compute method have to "write" their values directly into the records:
#api.multi
def compute_type(self):
context_type = self._context.get('type', True)
for record in self:
if context_type:
record.type = "situation"
else:
record.type = "final_count"
Aside from this, your solution should simply use default values for field type. First change your field to a normal char field:
type = fields.Char(string="Type", readonly=True)
Little hint: the parameter string isn't necessary in this example, because newer Odoo versions will use the field name to generate the labels (string becomes the field label) for example: name will get Name or partner_id will get Partner (_id will be dropped).
Now change the button context, use default_type:
<button name="%(action_view_task_make_situation)d"
string="Create work situation" type="action" states="open"
context="{'default_type': 'situation'}"/>
<button name="%(action_make_general_final_count)d"
string="Create Final General Count" type="action" states="done"
context="{'default_type': 'final_count'}"/>
The prefix default_ in combination with a field name, will be used in later default extraction for the creation of a record.
Just try adding "default_" before type in the context, like this:
context="{'default_type': False}"
and only use boolean because you validate boolean in compute_type, make this in the views too.

Keep order of many2many as insertion odoo 10

I have two model: A, and B. B has a many2many field linked to A (named x). A include some objects with ids: 1,2,3,4.
In B, I add items to x in order 3,2,4, but it always reorders to 2,3,4. I tried with many2many_tag widget but get the same results.
How can I keep the order of x like I have inserted.
Sorry for my bad English. Thanks
class A(models.Model):
_name = 'a'
name = fields.Char(string='Name')
class B(models.Model):
_name = 'b'
x = fields.Many2many('a',string='x')
And the view
<record id="view_doc_generate" model="ir.ui.view">
<field name="name">B</field>
<field name="model">b</field>
<field name="arch" type="xml">
<form >
<field name="x" mode="tree">
<tree>
<field name='name'/>
</tree
</field>
</form >
</field>
</record>
You can add the order you want A to be in by adding an _order attribute and using write_date:
class A(models.Model)
_name='my_module.A'
_order = "write_date asc"
That will bring the objects in the order you inserted them

How to Display / Restrict the part of many2one records to list in odoo?

view.xml
<field name="planning_id"/>
<field name="employee"/>
<field name="job_position" />
I want to display the job_position which is matched with the similar companies of planning_id(job_position.company_id=planning_id.company).
I tried with domain but its not working.
<field name="job_position" domain="'company_id'=planning_id.company_id"/>
First of all you need to create a related field to have the company in the same model, so, in the .py of your model:
company_id = fields.Many2one(related='planning_id.company_id')
Then, in the view:
<field name="job_position" domain="[('company_id', '=', company_id)]"/>

treeview with select option, to add selected in odoo

I need to create a popup treeview like the one in picture below, and then can select as many items as I need to be inserted to the original tree-view
If you want it to give you a popup containing a tree view where you can choose from the list. You need to have a many2many relation between the 2 modules, it seems like you have one2many relation.
if your field is a one2many field from what i understand your field must be a one2many field because in one2many field when you click on add item it send you direclty to a create popup in order to change this behavor you need to change the widget of this field like this :
<field name="you_o2m_field_name" widget="many2many" >
<tree>
<field name="field_name1"/>
<field name="field_name1"/>
<field name="field_name1"/>
</tree>
<form>
<!-- you can put a costum form view also here -->
</form>
</field>
but you i think you need to give it an option not_delete like this
<field name="you_o2m_field_name" widget="many2many" options="{'not_delete':True}"/>
because when you remove the record from the tree one2many delete it from the table if you want to keep it then use no_delete option
define a search view for the model exam
<record id="id_for_this_view_here" model="ir.ui.view">
<field name="model">model.name</field>
<field name="arch" type="xml">
<search string="recherche" >
<field name="field_name" />
<field name="field_name"/>
</search>
</field>
</record>
the frame work will create a search view for you if you don't specify one for your model this why the search have only create by ok so whenever you have a search view with create by you need to define a _rec_name for your model or create a search view

Resources