覆盖写函数

Overide write function

他在那里,我试图覆盖写入函数以更改 'fci_student' 模型和 'fci_standard' 模型中的学生 'standard',但运气不好:( 我能帮忙吗?

这是我的学生 table 模特:

from openerp.osv import osv, fields
from datetime import date
from dateutil.relativedelta import relativedelta


class fci_student(osv.osv):
    _name = 'fci.student'
    _columns = {
        'photo': fields.binary(string='Photo'),
        'name': fields.char(size=128, string='First Name', required=True),
        'middle_name': fields.char(size=128, string='Middle Name', required=True),
        'last_name': fields.char(size=128, string='Last Name', required=True),
        'status': fields.selection([('under_Age', 'Under Age'), ('get_card', 'Get Card'), ('got_card', 'Got Card')],
                                   string='Military Status'
                                   , default='under_Age'),
        # basic details
        'user_id': fields.many2one('res.users', 'User'),
        "birth_date": fields.date(string='Birth Date', required=True),
        'gender': fields.selection([('m', 'Male'), ('f', 'Female'), ('o', 'Other')], string='Gender', required=True),
        'title': fields.selection([('mr', 'Mr.'), ('ms', 'Mrs.')], string='Title'),
        'Paid': fields.boolean('Paid Student'),
        'nationality': fields.many2one('res.country', string='Nationality'),
        'id_number': fields.char(size=64, string='ID Card Number', required=True),
        'phone': fields.char(string='Phone Number', size=256, required=True),
        'email_address': fields.char(string='E-mail', size=256, required=True),
        'pay_amount': fields.char(size=64, string='Payment', required=True),
        # Edictional details
        'standard_id': fields.many2one('fci.standard', string='Standard', required=True),
        'group_id': fields.many2one('fci.standard.groups',string='Groups(s)'),

        'terms_id': fields.many2one('fci.terms', string='Term'),
        'sit_number': fields.integer(size=128, string='Sit Number',required=True),
        'subject_id':fields.many2many('fci.subject','student_sub_rel','sub_student_rel','sub_stu_id','Subjects'),
        # Parent details
        'parent_name': fields.char(size=128, string='Parent First Name'),
        'parent_middle_name': fields.char(size=128, string='Parent First Name'),
        'parent_last_name': fields.char(size=128, string='Parent First Name'),
        'parent_phone': fields.integer(size=128, string='Parent Number'),
        'parent_ids': fields.char(size=128, string='Parent E-mail'),
        # Library details
        'library_card_number': fields.char(size=64, string='Library Card Number'),
        'library_card_type': fields.char(size=64, string='Library Card type'),
        'number_of_books_allow': fields.char(size=64, string='Number of books allow'),
        # Address details
        'street_name': fields.char(size=128, string='Street', required=True),
        'city_name': fields.char(size=128, string='City', required=True),
        'state_name': fields.char(size=128, string='State', required=True),
        'Zip': fields.char(size=128, string='Zip Code'),
        'country': fields.char(size=128, string='country', required=True),
        #Exams
        'exam_res': fields.char(size=128, string='Results'),
        'grade': fields.char(size=128, string='Results'),
        'percentage': fields.char(size=128, string='Results'),

    }
#write function to edit student and move him from Standard A to standard B
   def write(self, cr, uid, ids, values, context = None):
       res = super(fci_student, self).write(cr, uid, ids, values, context = context)
       if values.get('standard_id'):
           self.pool.get('fci.standard').write(cr, uid, values['standard_id'], {'student_ids' : [(0, 0, values)]}, context=context)

       return res
#create function to add student to standard model
def create(self, cr, uid, vals, context=None):
        context = context or {}
        created_id = super(fci_student, self).create(cr, uid, vals, context=context)
        if vals.get('standard_id') and vals.get('group_id'):
            self.pool.get('fci.standard').write(cr, uid, vals['standard_id'],
                                                {'student_ids': [(4, created_id)]}, context=context)
            self.pool.get('fci.standard.groups').write(cr, uid, vals['group_id'],
                                                {'student_ids': [(4, created_id)]}, context=context)

这是我的标准 table 模型:

from openerp.osv import osv, fields

class fci_standard(osv.osv):
    _name = 'fci.standard'
    _order = 'sequence'
    _columns = {
        'code': fields.char(size=8, string='Code', required=True),
        'name': fields.char(size=32, string='Name', required=True),
        'sequence': fields.integer('Sequence'),
        'student_ids': fields.many2many('fci.student', 'student_standard_rel', 'fci_student_standard_id', 'standard_id',
                                        string='Student(s)'),
        'groups_ids': fields.many2many('fci.standard.groups', 'standard_group_rel', 'fci_standard_group_id', 'standard_group_id',
                                        string='Groups(s)'),

    }
fci_standard()

它仅更改了 'fci_student' 中的标准,但并未将学生从标准 'A' 转移到标准模型中的标准 'B '!

你定义了错误的学生和标准之间的关系, 在 fci.student 中有 standard_idmany2one 那么在 fci.standard 中必须有 one2many 关系对于 student_ids 那么只有这样你才会得到自动效果而你改变了任何学生的标准。

在您的情况下,无需重写写入方法,只需像下面这样更改字段定义,

'student_ids': fields.one2many('fci.student', 'standard_id','Student(s)'),

当你改变任何学生的标准时,你会看到双方的影响。您还需要重新启动服务器并更新模块。