Odoo Help


Python code error in onchange one2many field. How can i solve this?

on 10/8/14, 6:28 AM 1,797 views

class location_location(osv.osv):

      _name = 'location.location'
      _description = 'Site Information'
      _columns = {
                  'name': fields.char('Location'),
                  'location_ids': fields.one2many('project.location', 'project_id', 'Work done'),

class project_location(osv.osv):
      _name = "project.location"
      _description = "Project Location"
      _columns = {
        'project_id': fields.many2one('project.project', 'Project'),

Then i will enter the projects in the specified location.  When location change, its corresponding projects should be displayed in the project field(many2one field)

onchange function is

 'dest_location' : fields.many2one('location.location',"New Location"),
 'new_project' : fields.many2one('project.project','New Project'),

 def onchange_project_id(self, cr, uid, id, dest_location, context=None):
          if dest_location:
             project = self.pool.get('location.location').browse(cr, uid, dest_location, context=context).location_ids
             print "locationnnnnnnnnnnnnnnnn",project
             return {'value': {'new_project': project.name}} 


How can solve this?

Your 'project_id' field on the project location should point to your site information OR your location_ids on the site information should use a different field to point to in the project location. Easiest would be to create a many2one on your project.location, call it 'site_location_id' and point it to 'location.location' object.

21south, Ludo - 21South
on 10/8/14, 7:17 AM

Your */return type/* is not matching with the column/* */"new_project" data_type, do have a look at it 1. In you object: you have defined it as Many2one,/ (*which take integer as an input*)/ 2. In onchange: variable project is of type list, (*/list of browse_records/*) 3. similarily you are trying to fetch name from project /*(list variable)*/ which is not sinking with the semmantics....

on 10/8/14, 7:15 AM

I swear I just read the above comments as answers. o_O

Ben Bernard
on 10/9/14, 12:22 AM

@ Ben Bernard: That is correct. Furthermore, the replies on those answers have been removed as well. Odd situation.....

21south, Ludo - 21South
on 10/9/14, 8:56 AM

Hmmmm... I did answered it in Answer post... but guess my answer was not appropriate, hence somebody has converted it to a Comment .. funny :) :)

on 10/9/14, 8:59 AM



| 7 8 8
Thiruvananthapuram, India

Working as an Senior ODOO developer. http://in.linkedin.com/pub/remya-r/69/a23/298

On 10/9/14, 1:38 AM

Try this:


def onchange_location_id(self, cr, uid, id, new_project, context=None):
          if new_project:
             project = self.pool.get('project.project').browse(cr, uid, new_project, context=context)
             return {'value': {'dest_location': project.location.id}} 
          return {'value': {}}

About This Community

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


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

5 follower(s)


Asked: 10/8/14, 6:28 AM
Seen: 1797 times
Last updated: 3/16/15, 8:10 AM