Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

how i Populate Many2One field on change of another field on v8?

By
Abdallah Sahabn
on 6/4/15, 3:01 PM 665 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

please include view file...

Baiju
on 6/16/15, 4:15 AM
0
Jaakko Komulainen
On 6/5/15, 3:00 AM

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

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

Abdallah Sahabn
on 6/5/15, 7:50 AM

updated original answer

Jaakko Komulainen
on 6/5/15, 8:17 AM

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

Abdallah Sahabn
on 6/5/15, 12:52 PM
0

Baiju

--Baiju--
3154
| 5 4 8
Kochi, India
--Baiju--
ODOO technical and functional

        I like learning new thing, especially technical thing. I am a quick learner, I can learn technical things very fast. I have good logical skill,  I have immense interest in programming.

e-mail id : baijuks@hotmail.com

Baiju
On 6/5/15, 12:57 AM

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



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

Abdallah Sahabn
on 6/5/15, 7:51 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 6/4/15, 3:01 PM
Seen: 665 times
Last updated: 6/16/15, 4:15 AM