validation of different checkboxes in openerp
Domanda
I am newbie in OpenERP. I am working on CRM module. I am creating on Bug module, which is actually inherited by crm_claim
. I have put some additional fields(checkboxes into it). Its working fine.
My experience with OpenERP is going very well. But I am stuck at the point of validation. Actually what I want is that user can not save record if no checkbox is clicked. If one of them is clicked it should save the record. I have searched internet. I could not find any sure solution but some ambiguous ones. Here is the list:-
Some posts are suggesting to override write and create. But my question is, I am inheriting from crm_claim
, I did not find write and create functions anywhere in hierarchy. If I override them, I have to write all logic to save whole claim and above class as well. Which I feel very difficult.
- I tried to find
onsave
event but I did not find any. - Someone is suggesting wizard to resolve this issue.
I don't know what to do. Kindly help me in this regard.
Soluzione
You do not have to override create or write function. as overriding such a core function is not preferable unless and until you do not have any option .
in this case you have option .
please refer the sale module addons/stock/stock.py
you will find the _constraints, you can use the same . it will check only when selected fields' values will be change. also it have exception raise facility .
Altri suggerimenti
The write
and create
methods are inherited from osv.osv model which is implemented in the BaseModel
class in server/openerp/osv/orm.py
.
You do not need to to replicate the logic, you just need to call the original method.
For example, in your model, add the following method:
def create(self, cr, uid, vals, context=None):
"""
Override osv.create() method to validate the data
"""
if not (vals['attr1'] or vals['attr2'] or vals['attr3']:
raise osv.except_osv(_('Warning !'),_("You have to choose at least one attribute"))
return super(my_model, self).create(cr, uid, vals, context)