T.Nel T.Nel - 11 months ago 34
Python Question

Storing an int in odoo

I'd like to know if their is a way to store an integer somewhere in odoo, without having to create an new objet. I'd like to keep a number in order to show only partner with an id superior to that number. But I don't want to add a field in every single record of res.partner. How can I have a number I can acces into my xml without it being a field of res.partner?


You can use the model ir.config_parameter for such cases. Get used to the medhod set_param() and get_param().

The parameter holds his value as string, so for my example it will be parsed into integer. The xml id will be my_module.test_parameter.

But using this within the view is a bit tricky. A way would be a filter with a context "signal" within the search view of the model:

<filter name="my_filter" string="My Filter" context="{'search_by_parameter':1}" />

With that you could override the search() of your model to use the parameter when the filter was activated:

def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
    context = dict(context or {})
    if 'search_by_parameter' in context:
            value = int(self.pool.get('ir.config_parameter').get_param(cr, uid, 'my_module.test_parameter'))
            value = 0
        args.append(('id', '>', value))
    return super(my_model, self).search(cr, uid, args, offset, limit, order, context, count)