Openerp将另一个对象字段值与对象字段进行比较

时间:2012-12-16 10:12:19

标签: openerp

我有这个:

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'
    }

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

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

提前致谢!

4 个答案:

答案 0 :(得分:1)

你必须在'date'字段上写一个on_change函数,如果给定日期是冬季月份并且isWinter为false,则会引发错误。 当然,你必须在xml视图中的字段定义中定义iswinter和put on_change的范围日期。

答案 1 :(得分:1)

你可以覆盖create&写方法。在这些方法中,只需检查“inWinter”是True还是False。

将在创建新记录时调用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)

答案 2 :(得分:1)

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

答案 3 :(得分:0)

转到_constraints=,不要打电话给createwrite