Odoo Help


Field.function to store students strentght in a class

on 6/25/14, 7:58 AM 1,465 views

I have an object student.student

 in this object, i have defined a field

'applied_coursed': fields.function(_get_allcourses, method=True, string='Taken Classes',store=True, type="integer"),

which will simply counts a student courses (from table student.courses). Now i wan,t if a new course for the student is added to student.courses, this function should be automatically called to update field value in student.student. Same process should be innitated on deletion of a student course from student.courses.

Only method = True will not work here, 

How i can i achieve this? shoud store value will be a dictionary with key =student.courses?

Any idea?


René Schuster

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

Challenge Everything!

René Schuster
On 6/25/14, 8:19 AM

You are right.

Quote from https://doc.openerp.com/6.0/developer/2_5_Objects_Fields_Methods/field_type/#store-parameter:

store Parameter

It will calculate the field and store the result in the table. The field will be recalculated when certain fields are changed on other objects. It uses the following syntax:

store = { 'object_name': ( function_name, ['field_name1', 'field_name2'], priority) }

It will call function function_name when any changes are written to fields in the list ['field1','field2'] on object 'object_name'. The function should have the following signature:

def function_name(self, cr, uid, ids, context=None):

Where ids will be the ids of records in the other object's table that have changed values in the watched fields. The function should return a list of ids of records in its own table that should have the field recalculated. That list will be sent as a parameter for the main function of the field.

So change your store value to (something like):

store = {'student.courses': (_get_student_ids, ['student_ids'], 20)}

and write a function like:

def _get_student_ids(self, cr, uid, ids, context=None):
    ids = [];
    for course in self.pool.get('student.courses).browse(cr, uid, ids, context=context):
        ids.append([student.id for student in course.student_ids]);
    return ids;


If your student-course-relationship is bidirectional, you can write a lambda function like:

lambda self,cr,uid,ids,c={}: self.search(cr, uid, [('course_ids','in',ids)])



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

2 follower(s)


Asked: 6/25/14, 7:58 AM
Seen: 1465 times
Last updated: 3/16/15, 8:10 AM