Show a many2many field as a many2one - odoo

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

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')]}"}"/>

How to apply an domain on relational field odoo?

Here is my sale.order.line module, I do some modifications
I want to apply a domain on the product that not all product be displayed just products in Ligne contract field for example :
Here in my Contrat line, I Have just one Product so on the Sale order lines only this product (article) must be shown
Use onchane event for this in your sale.order.line
#api.onchange('contrat_id')
def set_domain(self):
# force the user to reselect the producg if he changes the contrat line
self.product_id = False
if self.contrat_id :
return {'domain': {'product_id': [('id', 'in', self.contrat_id.product_ids.ids)]}}
else:
# remove the domain if no contrat is selected
return {'domain': {'product_id': []}}
I'm using my phone sorry if i made a syntax error but I hope you get the idea
Edits
Okay in your contract model you don't have a many2many field to product model as I thought instead you have this one2many field ligne contract
So let suppose that the name of that field is ligne_ids in this one2many relation there is a many2one field to product model let us say its name is product_id.
Use the power of mapped to extract in one line all product ids in the contract lignes.
# many2one -> one2many -> mapped('many2one') this will collect of the records without duplication from the o2m field.
# contract -> contract lignes -> products
self.contrat_lignes_id.ligne_ids.mapped('product_id').ids
Hope this helps you

How to send multiple values in many2many field in odoo?

I am trying to generate purchase order from manufacturing order.I have created many2many field for getting multiple products.I want to send multiple product ids to my custom function.I am able to send 1 value but sending more than one gives error as Expected singleton: product.template(4, 3).
from openerp import models,fields,api
class generate_purchase_order(models.Model):
_name = 'mrp_to_purchase_order'
product_id = fields.Many2many('product.template',string='Products',required=True)
#api.multi
def generate_purchase_order2(self):
for wizard in self:
mrp_obj = self.env['mrp.production']
mrp_obj.generate_purchase_order(wizard.product_id.id) #function call
return { 'type': 'ir.actions.act_window_close'}
mrp_custom.py,
from openerp import models,api
class mrp_production(models.Model):
_inherit = 'mrp.production'
#api.multi
def generate_purchase_order(self,product_id):
purchase_line_obj = self.env['purchase.order.line']
context = self._context
for order in self.browse(context['active_ids']):
for line in order.bom_id.bom_line_ids:
if line.product_id.id != product_id:#problem line
continue
#rest of code
singleton: product.template(4, 3)
This error means that code is expecting single record not recordset, so you must change code to allow recordset using or make ensureone with try-catch and avoid errors. Thats general information.
Now if you want to get multiple records from many2many its not problem at all, you must pass this many2many object only and then work with it.
After getting many2many object to work with every record from this recordset you must use for record in recordset:
Also wizard.product_id.id this is error!!! product_id is many2many so you must pass product_id or if you want to browse by yourself you must pass product_id.ids

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)])

Auto Populate a Grid in Odoo and Error

Odoo do an Auto Populate a Grid when a user Creating a new information with One2many relations fields?
this is my Example Auto Populate
def getCheckListId(self):
self.env.cr.execute("select 1 employee_id,1 PARAM1,1 PARAM2,1 PARAM3,1 PARAM3,1 PARAM4 from hr_employee_checklist ")
checklistTemplates = self.env.cr.fetchall()
return checklistTemplates
And this function will be used as a default in One2ManyFields
employee_checklists = fields.One2many('hr.employee_checklist','employee_id', readonly=False,copy=False, default = getCheckListId)
But I have an error
the error is
AttributeError: 'str' object has no attribute 'iteritems'
Can someone help me with this problem or other ways to populate Grid in Odoo
One2many
One2many field; the value of such a field is the recordset of all the records in comodel_name such that the field inverse_name is equal to the current record.
Parameters
comodel_name -- name of the target model (string)
inverse_name -- name of the inverse Many2one field in comodel_name
(string)
domain -- an optional domain to set on candidate values on the client
side (domain or string)
context -- an optional context to use on the client side when
handling that field (dictionary)
auto_join -- whether JOINs are generated upon search through that
field (boolean, by default False)
limit -- optional limit to use upon read (integer)
So, One2many fields always contains comodel's reference value, you just need to give ids of that relational field, remaining things will maintain by odoo engine it self.
#api.model
def getCheckListId(self):
return self.env['hr.employee.checklist'].search([]).ids