I have a method that returns reference value from one of the objects. Before calling a method I don't know from which class object will be returned. And different class reference attribute is different, but it is used for same purpose.
It looks like this:
def _get_ref_object(self):
""""Return reference object: invoice or expense"""
self.line_ids.ensure_one()
line = self.line_ids[0]
invoice = line.move_line_id.invoice
if invoice:
return invoice
else: # If there is no invoice, it means it is expense
return line.expense_id
Now when I have exact object I can return various reference value from various attributes. For example if I need reference invoice or expense number, for invoice I need to return number attribute's value and for expense, name attribute's value.
But for this to work I need to call if/else again to know which object was returned, like:
def get_reference(self, ref):
obj = self._get_ref_object()
if ref == 'number':
if obj._name = 'account.invoice':
return obj.number
else:
return obj.name
Now if I would want to get reference for another attributes, I would need to do same if/else checks. But I started thinking, maybe use dictionary to map all of this and then just access attribute using ref as key? By performance wise, is it faster to just access dictionary's value instead of doing if/else? And if it is a good practice in such cases?
Aucun commentaire:
Enregistrer un commentaire