Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

A suggestion to add a new standard field to the ORM

By
Tarek Mohamed Ibrahim
on 4/10/16, 4:09 AM 320 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

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.

Bista Solutions US, Ray Carnes
on 4/10/16, 3:35 PM

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.

Tarek Mohamed Ibrahim
on 4/11/16, 3:24 AM
0

Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
896
| 5 3 7
Gîza, Egypt
--Tarek Mohamed Ibrahim--

I am an old VFP developer on ERP

I have moved to 2p since Nov-2014 and started developing with Python on Odoo.

https://www.linkedin.com/profile/public-profile-settings?trk=prof-edit-edit-public_profile

tibrahim@2p.com.sa

Tarek Mohamed Ibrahim
On 4/10/16, 4:22 AM


    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


Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 4/10/16, 4:09 AM
Seen: 320 times
Last updated: 4/10/16, 4:22 AM