python – 覆盖create方法以创建另一个模型的记录

我需要覆盖模型的create方法.我有两个型号.

1.’person_travel”

2.’person_calender”

我打算做的是当我为模型旅行创建一个记录时,它应该自动创建模型’person_calender’的记录.

我的最终目标是在创建旅行记录时,它有一个日期字段.我需要我的日历视图来显示该日期的事件.

这是我的第一个模特.

class Travel(models.Model):
 _name = 'person_travel'

name = fields.Char('Travel name',size=64,required=True)
package = fields.Many2one('package_travel','Package',required=True)
travel_Mode = fields.Selection((('a','Bus'), ('b','Van'), ('c','Car'), ('d','Flight'), ('e','Train'), ('f','Ship'), ('g','Motor cycle'), ('h','Other')),'Travel mode')
from_Place = fields.Char(size=64)
To_Place = fields.Char(size=64)
status = fields.Selection((('a','Pending'), ('b','Planned'), ('c','Done')),'Status')
start_date = fields.Date('Start date')
end_date = fields.Date('End date')
ticket_number = fields.Char(size=64)
seat_number = fields.Char(size=64)
description = fields.Text(size=64)
one_to_many = fields.One2many('carry_item','one_to_many')

@api.model
def create(self, values):
    print values

这是我的第二个模特….

class Calender(models.Model):
_name = 'person_calender'

start = fields.Date('start')
# stop = fields.Datetime('stop')
state = fields.Many2one('person_manage')

subject = fields.Char(size=64)
category = fields.Selection((('a','Important'), ('b','Business'), ('c','Personel'), ('d','Business'), ('e','Travel'), ('f','Medical'), ('g','Finance'),('h','Anniversary'),('i','Birthday')),'category')
description = fields.Text(size=64)`

解决方法:

他们需要解决大量的错误.命名约定是完全错误的.字段字符串和许多人comodel_name等等……你需要修复.

无论如何你看到我已经添加了关于如何创建一些记录创建记录的代码.这将为您提供完成记录字段值的想法.

class Calender(models.Model):
   _name = 'person.calender'

   start_date = fields.Date(string='Start Date')
   stop_date = fields.Datetime(string='Stop Date')
   state = fields.Many2one(comodel_name='person.manage')
   name = fields.Char(size=64, string="Subject")
   category = fields.Selection((('a','Important'), ('b','Business'), ('c','Personel'), ('d','Business'), ('e','Travel'), ('f','Medical'), ('g','Finance'),('h','Anniversary'),('i','Birthday')),'category')
   description = fields.Text(size=64)

class Travel(models.Model):
    _name = 'person.travel'

    name = fields.Char('Travel name',size=64,required=True)
    package = fields.Many2one('package_travel','Package',required=True)
    travel_Mode = fields.Selection((('a','Bus'), ('b','Van'), ('c','Car'), ('d','Flight'), ('e','Train'), ('f','Ship'), ('g','Motor cycle'), ('h','Other')),'Travel mode')
    from_Place = fields.Char(size=64)
    To_Place = fields.Char(size=64)
    status = fields.Selection((('a','Pending'), ('b','Planned'), ('c','Done')),'Status')
    start_date = fields.Date('Start date')
    end_date = fields.Date('End date')
    ticket_number = fields.Char(size=64)
    seat_number = fields.Char(size=64)
    description = fields.Text(size=64)
    one_to_many = fields.One2many('carry_item','one_to_many')

    @api.model
    def create(self, vals):
        record = super(Travel, self).create(vals) 
        if record.start_date  and record.end_date:
            self.env['person.calender'].create({
                'start_date': record.start_date,
                'end_date': record.end_date,
                'name': record.name,
            })
        return res_id

贝斯茨

上一篇:如何在OpenERP7中使用继承属性?


下一篇:多次调用计算方法?