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

Onchange on one2many field error

By
Nanda Kanoko
on 7/16/13, 12:35 AM 2,327 views

I'm trying to add records to my one2many fields

Py :

class employee_department(osv.osv):

    _name = "employee.department"
    _description = "Employee Department"
    _columns = {
        'deprt_id' : fields.many2one('hr.department', 'Department'),
        'my_emp_ids' : fields.one2many('hr.employee', 'department_id','Employee'),

        }
def onchange_department(self, cr, uid, ids, deprt_id):
    employee_ids = self.search(cr, uid, [('department_id', '=', deprt_id)])
    return {'value': {'my_emp_ids': employee_ids}}

employee_department()

My problem is that :

When i don't include onchange_department in my xml , my o2m fields always shows the employee assigned to no. 1 ID in my department , and so on when i save it . Example : Let's say i got 3 dept , with Admin as ID no 1 , Developer as ID no 2 and Engineer as ID no 3 . When i create it for the first time , even though the department shows at Developer , the employee printed is always Admin , it apply to next create , that always show employee in Developer and Engineer dept . And when i create for the 4th time , it doesn't show any employee at all

If i include onchange_department in my xml , i always got AttributeError: 'employee_department' object has no attribute 'onchange_department' . My xml is obviously wrong so i also need help on xml as well . Thanks in advance

my xml ( form)

<form string="Employee Department"> 
<field name="deprt_id" on_change="onchange_department(deprt_id, my_emp_ids)"/>
<notebook position="before">
<page string="Members">
<field name="my_emp_ids"/>
</page>
</notebook>
</form>

about not having department_id in my class , I thought that it already covered by deprt_id since it relate to hr.department , while department id also relate to hr.department as well

1

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 12/12/13, 10:24 AM

The mistake might be in line:

employee_ids = self.search(cr, uid, [('department_id', '=', deprt_id)])

Since you want to search for employees you should use the search method on the employee model:

employee_ids = self.pool.get('hr.employee').search(cr, uid, [('department_id','=',deprt_id)]

Your model employee.department obviously has no column 'department_id'.

0

Grover Menacho

--Grover Menacho--
2523
| 5 7 8
La Paz, Bolivia
--Grover Menacho--

Systems Engineer + MBA

Grover Menacho
On 7/16/13, 12:59 AM

Maybe it's because you have a mistake.

If the code that you posted is ok. It's wrong. You have to tab your function so it's going to be inside the class. When you leave it on the first column like the class it's not inside the class and it's not going to work

thanks for reply . Can you explain to me how to do that ? thanks

Nanda Kanoko
on 7/16/13, 1:16 AM

use the "tab" key

Gilles Major (gim)
on 7/16/13, 12:37 PM

before your def, preferrably

Gilles Major (gim)
on 7/16/13, 12:37 PM

That's right. def onchange... and the two lines below. Press tab on each of them. Python is based on tabs.

GH Mediacloud, Grover Menacho
on 7/16/13, 2:03 PM

Thanks for the kind reply , I've tried the tab , but still got an attribute error .. Is there any other mistakes beside the tab function ?

Nanda Kanoko
on 7/16/13, 11:33 PM

you need to call onchange function in xml for deprt_id field and in this line employee_ids = self.search(cr, uid, [('department_id', '=', deprt_id.id)])

Maniganda
on 7/17/13, 4:06 AM

Thanks for reply , I've tried it but it give me another AttributeError: 'int' object has no attribute 'id'

Nanda Kanoko
on 7/17/13, 4:52 AM

can you post the class and xml code here and your class has department_id as field??

Maniganda
on 7/17/13, 4:55 AM
0
Subhin P V
On 12/13/13, 6:01 AM

Your XML should be Like This:

 <form string="Employee Department"> 
<field name="deprt_id" on_change="onchange_department(deprt_id)"/>
<notebook position="before">
<page string="Members">
<field name="my_emp_ids"/>
</page>
</notebook>
</form>

And your Python code Should be Like This:

class employee_department(osv.osv):

    _name = "employee.department"
    _description = "Employee Department"
    _columns = {
        'deprt_id' : fields.many2one('hr.department', 'Department'),
        'my_emp_ids' : fields.one2many('hr.employee', 'department_id','Employee'),

        }
    def onchange_department(self, cr, uid, ids, deprt_id):
         employee_ids = self.search(cr, uid, [('department_id', '=', deprt_id)])
         return {'value': {'my_emp_ids': (4,employee_ids)}}

 employee_department()

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: 7/16/13, 12:35 AM
Seen: 2327 times
Last updated: 3/16/15, 8:10 AM