Odoo Filter many2one field on load - odoo

In my module I have a many2one field to select workers for a particular task. According to the requirement that field should only display the workers in the current user's department. Simply this is the code,
_columns = {
'employee_id': fields.many2one('hr.employee', 'Employee'),
}
My problem is how to perform such filteration for a field on load? I tried using functional field in a domain in view xml. but It seems functional field gets its value when saving the particular record.
Also I tried adding domain to the field itself, here get_current_user_department is a function returns the department id
_columns = {
'employee_id': fields.many2one('hr.employee', 'Employee',domain=[('department_id.id','=',get_current_user_department)]),
}
This generates following error,
TypeError: is not JSON serializable
Any suggestion to make this work? Thanks

Also you can take one field for storing current user department you can set default value of current user department.
default_department_id = fields.Many2one('employee.department',
string='My User',
default='get_department')
Now you need to create function for set default department.
After that you need to write in XML:
<field name="default_department_id" invisible="1"/>
<field name="employee_id"
domain="
[('department_id','=',default_department_id)]
"/>

You have to define a new many2one field to save the current user department ID and put the value of the department at loading with default_get() method.
Then after, you can put this field on a domain to filter employee that are in the same department than the user.

Related

Restrict write permissions for a field - Odoo 15

How can I restrict the write permissions for a field to a specific group ?
I want to check if a user is in a specific group with id 46. If the user is in this group, he should be allowed to write in this field. If he is not in this group, he should not be allowed to write.
The field is a custom field, editing the domain with the studio app I think I should avoid.
My field:
<field name="customer_codename" placeholder="Codename" attrs="{'invisible':['|',('customer_rank','=', 0),('is_company','=', False)]}"/>
I tried the following, but it did not work:
I created a new field using the studio app. Field type is boolean.
In the advanced properties I wanted to define the compute for the field. In dependencies I gave "user_id" and in the compute field I gave
for record in self:
user_id.has_group('__export__.res_groups_46_eff9dc52')
The boolean field should be set to true if the user is in a certain group.
Not sure if I can give you the best answer there is.
But for me, I'd personally create a Boolean field in the view's associated model, with its default field a lambda function checking if the user belongs to the groups you mentioned.
Assuming groups_id is the name of the user groups in model res.users, we have:
class ResUsers(models.Model):
_inherit = "res.users"
can_write_codename = fields.Boolean(default=lambda self: self.groups_id in ("model_name.group_name"))
Then in your xml file, you can include can_write_codename inside attrs, like this:
<field name="customer_codename" placeholder="Codename" attrs="{'invisible':['|',('customer_rank','=', 0),('is_company','=', False)], 'readonly': [('can_write_codename', '=', 'True')]}"}"/>

Show a many2many field as a many2one

I have a field many2many and in a specific view I need to show it as a many2one, or imitate the behavior of the many2one fields (restrict that only one record can be added and if the user select another record, the one that had previously selected will be deleted). In the view I declared:
<field name="employee_ids" widget="many2one" />
but it gave me the following error:
TypeError: 'int' object is not iterable
Is there any way to achieve this?
I think you can force the user to select only one record by
using onchange decorator:
#api.onchange('employee_ids')
def force_one_selection(self):
"""Force the user to select only one record"""
if self.employee_ids and len(self.employee_ids) > 1:
# user has added a new record
self.employee_ids = [(6, 0, self.employee_ids[0].id)] # you can change the index to 1
# and you can return a warning here to tell the user that he should not select more than
# one record

access to many2one fields

I have a many2one field like this
state = fields.Many2one ("ags.traffic.operation.state")
state has the following fields
name = fields.Char
sequence = fields.Integer
type = fields.Selection
in my view i have
<field name = "state" widget = "statusbar" clickable = "True" / >
how can i access those fields to set a default value?
if you want to define the field that gets shown in the drop-down in your view in your model define _rec_name this tells odoo to display that field (in a drop down or in a many2many tag field) when a many2one or one2one relationship is created between that model and another model. for example if you want the sequence number to display in the drop down just set
_recname = 'sequence'
but by default odoo checks the model's field and if it finds a name field (just like you have defined in your model). it uses that as the default display name.
if you want to search records in odoo you can use the search method. please see the documentation for more information about the odoo ORM
https://www.odoo.com/documentation/8.0/reference/orm.html#openerp.models.Model.browse
search(args[, offset=0][, limit=None][, order=None][, count=False])
but a typical example is
search_records = self.env['your.model'].search([('id', 'in', ids)])

How to get create and edit option in dropdown in openerp

i have separate drop down field and at present i have given static values. i need to get dynamic values like a user should able to add the values and should show in the drop down.
Thanks in advance.
You can define many2one field for this purpose.
_columns = {
'your_field_name' : fields.many2one('object', string='My field')
}
Example
_columns = {
user_id = fields.Many2one('res.users', string=u"User")
}
So you will got list of users. Whatever object list you can define many2one field.

Openerp 7 many2one dropdown should display field of related record

If you install Openerp 7 with recruitment module. And create a simple entry with following values e.g.
Subject (internal field name = 'name') = 10 Year Experience
Applicant Name = Jhon Smith
Then if you create a custom module with following columns
_columns = {
'applicant_id': fields.many2one('hr.applicant', 'Applicant', required=True),
}
The view widget by default will show a drop-down with the Subject (internal field name ='name') field but i want to show applicant name (internal field name='partner_name') field in drop down, when creating a new record in my custom module.
In Summary how can I display Applicant's Name instead of Subject in drop-down widget in my custom module.
In openerp there is a function called name_get().This function returns a list of tuples containing ID of the record and name tobe displayed. So override this function and return list of tuples containing ID of the record and applicant name
You need to define applicant_id in _rec_name in your custom module.
Try this:
_rec_name = 'applicant_id'
Have a look at Predefined fields.