1

我有这个:

class events_places(osv.osv):

    """Places for events"""
    _name = 'oevents.places'
    _columns = {
        'name': fields.char('Name',size=35, help='Place\'s name', required = True),
        'description': fields.char('Description',size=50, help='Place\'s description'),
        'street': fields.char('Street',size=35, help='Place\'s street', required = True),
        'number': fields.integer('Local number', help='Place\'s local number', required = True),
        'zip': fields.char('Zip Code', size=5, help='Place\'s Zip code', required = True),
        'city': fields.char('City',size=20, help='Place\'s city', required = True),
        'country': fields.many2one('res.country', 'Country', help='Place\'s country'),
        'state': fields.many2one('res.country.state','State', help='Place\'s state'),
        'inWinter': fields.boolean('Active in winter', store = True, help='Place\'s province'),
    } 
    _defaults = {
        'inWinter' : True,
    }

class events_events(osv.osv):

    """Client's contacts"""
    _name = 'oevents.events'
    _columns = {
        'name': fields.char('Name',size=20, help='Event\'s name', required = True),
        'place': fields.many2one('oevents.places','Place', help='Event\'s location', required = True),
        'artist': fields.many2one('oevents.artists','Artist', help='Artist\'s performing in the event.', required = True),
        'client': fields.many2one('res.partner','Client', help='Event\'s clients.', required = True),
        'date': fields.date('Date', help='Event\'s date.', required = True),
        'type': fields.selection([('children','Children\'s'),('private','Private'),('concert','Concert')],'Event type', help='Type of event this artist can do'),
    } 
    _defaults = {
        'type' : 'private'
    }

当我想创建一个事件时,有一个与地点相关的字段。该事件有一个日期,但如果事件中的相关地点字段未选中 inWinter 字段,它不应该让我在冬季日期创建事件。

我怎样才能做到这一点?我需要创建一个放置在Winter 字段中的函数或约束并将其与日期进行比较,但我不知道该怎么做。有什么建议么?

提前致谢!

4

4 回答 4

1

您可以覆盖创建和写入方法。在这些方法中,只需检查“inWinter”是真还是假。

创建新记录时将调用 def create() 方法。

def create(self, cr, uid, vals, context=None):
    if vals.get('place'):
        event_brw = self.pool.get('oevents.places').browse(cr, uid, vals.get('place'), context=context)
        #if inWinter is True
        if event_brw.inWinter:
            raise osv.except_osv('Error ! ', 'You can not create an event in winter.')
    return super(oevents_events, self).create(cr, uid, vals, context)

修改记录时将调用 def write() 方法。

def write(self, cr, uid, ids, vals, context=None):
    if vals.get('place'):
        event_brw = self.pool.get('oevents.places').browse(cr, uid, vals.get('place'), context=context)
        #if inWinter is True
        if event_brw.inWinter:
            raise osv.except_osv('Error ! ', 'You can not create an event in winter.')
    return super(oevents_events, self).write(cr, uid, ids, vals, context)
于 2012-12-17T06:37:03.590 回答
1

您必须在“日期”字段上编写一个 on_change 函数,如果给定日期在冬季月份并且 isWinter 为假,则在该字段中引发错误。当然,您必须为 iswinter 定义范围日期,并在您的 xml 视图中的字段定义中添加 on_change。

于 2012-12-16T12:41:08.573 回答
1

你应该使用约束。grep addons 文件夹_constraints_sql_contraints你会发现很多例子。

于 2012-12-17T08:32:10.707 回答
0

去争取_constraints=,打电话根本不是好create主意write

于 2012-12-26T12:31:33.393 回答