Skip to Content
Menu
This question has been flagged
1 Reply
2560 Views

I have a suggestion to add a new standard field 'is_default', which would be used to set the default record in a table, for example, if I have a table of fiscal years, and need to set the current default year I would use this record, which will be changed by the end of the year to select another record.

How to do that

Avatar
Discard

But you can ALWAYS calculate the current fiscal year by using the CURRENT DATE and finding the year it is inside. A new field needs to be updated when the year changes.

Author

Fiscal year is just an example, I needed to create a generic solution for the case of setting a record in a table as a 'Default' record.

Author Best Answer


    add a new field to the base model, call it 'is_defaul'

    _columns = {

        ...

       'is_default': fields.boolean('Default', ),

       ...

       }

       

create a new method  'set_default_record'

    def set_default_record(self, cr, uid, ids, vals):

        if 'is_default' in vals:

            if vals.get('is_default'):

                query = 'update your_module set is_default=False where id<>%s'%(ids[0],)

                cr.execute(query)

            

call this method from the create and the write methods

    def create(self, cr, uid, vals, context=None):

        context = context or {}

        res_id = super(your_module, self).create(cr, uid, vals, context=context)

        self.set_default_record(cr, uid, [res_id], vals)

        return res_id

        

    def write(self, cr, uid, ids, vals, context=None):

        context = context or {}

        res = super(your_module, self).write(cr, uid, ids, vals, context=context)

        self.set_default_record(cr, uid, ids, vals)

        return res


Avatar
Discard