gorri
gorri

Reputation: 13

Openerp Comparing another object field value with an object field

I have this:

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

When I want to create an event, there's a place related field. The event has a date, but it shouldn't let me create the event in a a winter date if the related place field in the event has the field inWinter unchecked.

How can I do that? I need to create a function or constraint which gets place inWinter field and compare it with the date, but I don't know how to do it. Any suggestions?

Thanks in advance!

Upvotes: 1

Views: 938

Answers (4)

Sudhir Arya
Sudhir Arya

Reputation: 3743

You can override create & write method. In those methods just check whether "inWinter" is True or False.

def create() method will be called when new record will be created.

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() method will be called when record will be modified.

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)

Upvotes: 1

ifixthat
ifixthat

Reputation: 6295

Go for _constraints= , Is not at all good idea to call create and write.

Upvotes: 0

simahawk
simahawk

Reputation: 2431

you should use constraints. grep addons folder for _constraints and _sql_contraints and you'll find a lot of examples.

Upvotes: 1

user1888049
user1888049

Reputation: 41

you have to write an on_change function on 'date' field, where you raise an error if given date is in winter months and isWinter is false. Of course, yo have to define range date for iswinter an put on_change in field definition in your xml view.

Upvotes: 1

Related Questions