Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
8146 Lượt xem

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
Ảnh đại diện
Huỷ bỏ

please include view file...

Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

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

updated original answer

Tác giả

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

Câu trả lời hay nhất

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............



Ảnh đại diện
Huỷ bỏ
Tác giả

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

Bài viết liên quan Trả lời Lượt xem Hoạt động
6
thg 6 21
25397
1
thg 4 20
7351
2
thg 1 20
3692
0
thg 2 19
3508
0
thg 12 18
13