Is it possible to change the value of a selection field dynamically in Odoo 10? - odoo

I would like to have my selections depend on the value of a Char field, for instance, a Char field defined as such:
my_char = fields.Char("Enter Something", readonly = False)
so I suppose the selection field should call a function, something like "_get_value"
my_selection = fields.Selection(selection = ' _get_value')
#api.model
def _get_value(self):
my_list = [('key1','value1')]
#no idea how to assign the value of my_char to value1
return my_list
Eventually, I would like to have the selections in the drop down list vary as the user input different strings in my_char.
Is this achievable in Odoo? Because if it's not, I should better start reorganizing my structure. Thanks a lot.

As far is i know, it isn't possible with field type Selection. But you can use a Many2one field for such a behaviour.
class MySelectionModel(model.Models):
_name = "my.selection.model"
name = fields.Char()
class MyModel(models.Model):
_name = "my.model"
my_char = fields.Char()
my_selection_id = fields.Many2one(
comodel_name="my.selection.model", string="My Selection")
#api.onchange("my_char")
def onchange_my_char(self):
return {'domain': {'my_selection_id': [('name', 'ilike', self.my_char)]}}
Or without a onchange method:
my_selection_id = fields.Many2one(
comodel_name="my.selection.model", string="My Selection",
domain="[('name', 'ilike', my_char)]")
To let the Many2one field look like a selection, add the widget="selection" on that field in the form view.
How the domain should look like, should be decided by you. Here it is just an example.

No need to write method here. Just declare the dictionary to a variable and call it in selection field.
VAR_LIST = [('a','ABC'),
('p','PQR'),
('x','XYZ')]
my_selection = fields.Selection(string="Field Name",VAR_LIST)

Related

How to extend search record to also look at custom Many2Many field(s)?

In this image, there is a product search record that will search for name and default_code. I need to make it so that it will also look at my custom Many2Many field.
This is the field in the inherited model.
product_list = fields.Many2many("product.list", string="Product List")
The custom model only has _name, _description, and name variables.
The question is how to make the search to also look at all of the possible Many2Many data of this field.
I have tried this in the inherited model:
#api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
res = super(product_template_inherit, self).name_search(name='', args=None, operator='ilike', limit=100)
ids = self.search(args + [(name, 'in', 'product_list.name')], limit=limit)
if ids:
return ids.name_get()
return res
Nothing happens to the search. It still searches using the same behavior regardless of the code above.
Summary: I need to be able to search product by product list (custom Many2Many field inherited in the product.template model)
=============================================
UPDATE
Current code from what I have been trying is now this.
#api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
args = args or []
if operator == 'ilike' and not (name or '').strip():
domain = []
else:
domain = ['|', ('name', 'ilike', name), ('product_list.name', 'ilike', name)]
product_ids = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
return self.browse(product_ids).name_get()
However, it looks like it still searches using the same old fields. It does not change to behave as my function is written.
You can compute the search domain then return the result of the _search method.
The fleet module already uses the same logic to search vehicles using the driver name, you have just to replace the driver_id with product_list:
class ProductProduct(models.Model):
_inherit = 'product.product'
#api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
args = args or []
if operator == 'ilike' and not (name or '').strip():
domain = []
else:
domain = ['|', ('name', operator, name), ('product_list.name', operator, name)]
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)

How to update a field in Model B using onchange in the field of model A ? Odoo 12

What i'm trying to achieve is ,When updating a field in model A ,i needs to update a field in model B using onchange method
_name = 'Model_A'
health_profile = fields.Many2one('health.profile', domain="[('partner_id', '=', partner_id)]", string="Health Profile")
#api.onchange('health_profile')
def get_health_profile_specialist(self):
ctx = self.health_profile.id
res = self.env['model_B'].browse(ctx)
return res.update({'specialist_name': self.specialist_name})
From what I understand there is no need to modify the field with an onchange. You can directly check the value in the model A field, you could directly use a related field
health_profile = fields.Many2one('health.profile', domain="[('partner_id', '=', partner_id)]", string="Health Profile")
specialist_id = fields.Many2one('<yourmodel>', related='health_profile.specialist_id')
Maybe you should explain yourself better.

how get id with onchange for filtering

how can i retrieve the value of a many2one field or its ID from another model
for exemple:
class Contrat(models.Model):
_name = 'facturation.contrat'
contrat_parent_id = fields.Many2one('facturation.contrat', string='Numéro Contrat Client',
domain=[('is_prestataire', '=', False)])
class Lot(models.Model):
contrat_id = fields.Many2one('facturation.contrat', ondelete='cascade')
articlecontrat_ids = fields.Many2many('facturation.articleouvrage',string='Article Lot')
91/5000
i want that when i change contrat_parent_id i get it back to use it and filter my articles for field 'articlecontrat_ids'
here you need to use onchange event i'm assuming that facturation.articleouvrage have a m2o field named contrat_id
# in onchange event always put the name of the field that trigger the event
#api.onchange('contrat_parent_id ')
def onchange_contrat(self):
"""update the domain when we change the contrat"""
if self.contrat_parent_id :
# always check if the field is not empty
# return the domain like this but i don't know what you need exactly
return {'domain': {'articlecontrat_ids ' : [('contrat_id ', '=', self.contrat_parent_id.contract.id)]}}
else: # remove the domain
return {'domain': {'articlecontrat_ids ' : []}}
if you want to remove all records when user change the contrat_id but i think you make the user ungry
to reselect all this records.
self.articlecontrat_ids = [(5, 0, 0)]

Copy last value in new tree view row odoo 9

When click on Add an Item in tree view, I want in new row copy value from last inserted row.
Eg. if field name = 'Text' in new row I need in field name string 'Text'
Any simple solution?
If you want to load default value from a database then follow this method.
You can achieve it by overriding default_get method and in that, you need to write your logic.
#api.model
def default_get(self,fields):
res = super(class_name, self).default_get(fields)
last_rec = self.search([], order='id desc', limit=1)
if last_rec:
res.update({'your_field_name':last_rec.field_value})
return res
While you click on add an item it will fill the new record with its default value and in default value we have written last record's value it it's there.
If you want to load default value from list view (last added value in a list) then it's a bit tricky work, for that you can do something like as follow.
Add one field in the parent form.
last_added_value = fields.Char("Last Added Value")
Create onchange method for that field.
#api.onchange('field_name')
def onchange_fieldname(self):
# there must be many2one field of parent model, use it here.
self.parent_model_field.last_added_value = self.field_name
And in xml field, you need to write like this.
<field name="one2many_field" context="{'default_field_name' : parent.last_added_value}">
<tree string="Title" editable="bottom">
<field name="field_name"/>
</tree>
</field>
You also need to write default_get method.
#api.model
def default_get(self,fields):
res = super(class_name, self).default_get(fields)
last_rec = self.search([('parent_field_id','=',self.parent_field_id.id)], order='id desc', limit=1)
if last_rec:
res.update({'your_field_name':last_rec.field_value})
return res

How to assign a value of selection field to other selection field in a onchange method in odoo?

Just working on the following code to autofill a Selection field
calendar.event has a location field which is a selection field, trying to autofill it in my custom module based upon an onchange method.
I wanted to get the selected value in that selection field for a particular record into 'loc' field which is also a selection field in my custom module
def get_meet_dets(self, cr, uid, ids, meet_ref, context=None):
val = {}
res = []
if meet_ref:
for det in self.pool.get('calendar.event').browse(cr,uid,meet_ref,context=context):
for asst in det.attendee_ids:
emp_id = self.pool.get('hr.employee').search(cr, uid, [('user_id','in',user_id)])
val = {
'empname' : emp_id[0],
'wk_mail': asst.partner_id.email,
'loc' : det.location,
}
res.append(val)
val.update({'matp':res})
and 'loc' is a selection field in current class. Anyone having any idea on this?
You need to pass an existing id for your loc field, you can try 'loc' : det.location.id,. I hope this can be helpful for you.