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 to return one2many field in onchange function?

By
rosey
on 10/15/14, 2:27 AM 1,414 views

I have two fields, both are many2one

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

 <field name="dest_location" on_change="onchange_project_id(dest_location)"/>
 <field name="new_project" on_change="onchange_location_id(new_project)" />

My doubt :

Error in

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
       return {'value': {'new_project': project.name}}

How to return one2many field in a onchange function?

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'),
        
    }
location_location()

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

How can i import all projects in the location table when onchange works?

2

Anil R. Kesariya

--Anil R. Kesariya--
4321
| 6 5 8
Gandhinagar, India
--Anil R. Kesariya--
ERP Consultant

Key Skill
-
Technical & Functional Expert
Anil R. Kesariya
On 10/15/14, 3:04 AM

Hello rosey,

You need to make list of tuples for your one2many field and return it in your result dictionary with one2many field

Here you go!

return {'value': {''location_ids'': [(0, 0,  { fields }), (0, 0,  { fields })]}

Hope this will help you.

Regards,

Anil.

Thanks Anil. I will try.

rosey
on 10/15/14, 3:33 AM

its not working

rosey
on 10/15/14, 5:27 AM

Use this format (0, 0, { fields }) list tuples. each dictionary will contain your line data.

Anil R. Kesariya
on 10/15/14, 5:39 AM

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

rosey
on 10/15/14, 5:42 AM

is this correct??

rosey
on 10/15/14, 6:01 AM

Nop. let say you have three fields inside project.location model like name,date now your return value for number location_ids will be like this. return {'value': {'location_ids': [(0,0,{'name':"project_first", 'date':"project_date"}), (0,0, {'name':'project_second', 'date':"date_second"})] }} so on..

Anil R. Kesariya
on 10/15/14, 6:20 AM

Nop. let say you have two fields inside project.location model like name,project_id now your return value for number location_ids will be like this. return {'value': {'location_ids': [(0,0,{'name':"project_first", 'project_id':id of your project.}), (0,0, {'name':'project_second', 'project_id':id of your project })] }} so on..

Anil R. Kesariya
on 10/15/14, 6:23 AM
0

Ben Bernard

--Ben Bernard--
1083
| 4 3 6
Jakarta, Indonesia
--Ben Bernard--
Ben Bernard
On 10/15/14, 5:56 AM

return {'value': {'new_project': project.name}}

project is plural, but new_project is singular, so which project should return? If you want to return the first project, then

return {'value': {'new_project': project[0].id}}

Hmm, I think I should edit also the other answer from related question -___-

i want to return all projects, which have location the same. If location 1 have 3 projects, i want to select all of the three only in the list.

rosey
on 10/15/14, 7:07 AM

You can't do it, because new_project is many2one.

Ben Bernard
on 10/15/14, 8:47 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: 10/15/14, 2:27 AM
Seen: 1414 times
Last updated: 3/16/15, 8:10 AM