Skip to Content
Menu
This question has been flagged
2 Replies
7269 Views

if i have model project that have one or more from model unit , then i have another model that i want to select project then on change the project want to populate the units according to project selection 

example 

have project 1 have units 1,2 ,3 

project 2 have units 1,4,5 


when select project want to populate the units 1,2,3 if changed to project 2 populate  the units 1,4,5

i have written my own code to get the list of related units, just want to know how i can set this list to the units 


here is the code


class rsproject(models.Model):
    _name = 'rs.project'
    units = fields.One2many('rs.unit','name',string ='Units', ondelete='cascade')

class rs_unit(models.Model):
    _name="rs.unit"
project = fields.Many2one('rs.project', ondelete='cascade', string="Project", required=True)

class rsreservation(models.Model):
    _name = 'rs.reservation'
    project = fields.Many2one('rs.project', ondelete='cascade', string="Project", required=True)
    unit = fields.Many2one('rs.unit', ondeletel='cascade', string="Unit Number", required=True)

    @api.one
    @api.onchange('project')
    def on_change_project(self):
	    units_obj = self.pool.get('rs.unit') 
		project_id =  self.project.id
        #get units list according to selected project 		
		unit_list = units_obj.search(self._cr,self._uid,[('id','=',project_id)])
		# what is the correct syntax to set the unit field with unit_list
Avatar
Discard

please include view file...

Best Answer

Hi, you can populate your many2one field like this (assuming "related_units" is the name of the m2o field):


return = { 
    'value': { 'related_units': [(6,0,list_of_related_units)] }
}

Edit: 

You should check this: https://www.odoo.com/forum/how-to/developers-13/what-should-onchange-methods-do-and-return-57760

Avatar
Discard
Author

please see the updated question with the code, and let me know where i can put your suggestion

updated original answer

Author

i returned the domain but it is not working also return {'domain': {'unit':[('id','=',unit_list)]} }

Best Answer

Hi,

You can use domain filter for your need....

In ODOO domain filter, it will be written as:

syntax : Each tuple in the domain has three fields -> (‘field_name’, ‘operator’, value)

field_name : a valid name of field of the object model or in the database table

operator : valid operators are =, !=, >, >=, <, <=, like, ilike, in, not in, child_of, parent_left, parent_right (openerp/osv/expression.py)

value : a valid value to compare with the values of field_name, depending on its type.


You can use this;

domain = [(‘field1′,’=’,field2)] 

you can use filter for your unit with project.


Hope this helps............



Avatar
Discard
Author

please see the updated question with the code, and let me know where i can put your suggestion

Related Posts Replies Views Activity
6
Jun 21
23423
1
Apr 20
6024
2
Jan 20
3018
0
Feb 19
2599
0
Dec 18
13