How can I handle dynamic calculated attributes in a model in Django?
- by bullfish
In Django I calculate the breadcrumb (a list of fathers) for an geographical object. Since it is not going to change very often, I am thinking of pre calculating it once the object is saved or initialized.
1.) What would be better? Which solution would have a better performance? To calculate it at _init_ or to calculate it when the object is saved (the object takes about 500-2000 characters in the DB)?
2.) I tried to overwrite the _init_ or save() methods but I don't know how to use attributes of the just saved object. Accessing *args, **kwargs did not work. How can I access them? Do I have to save, access the father and then save again?
3.) If I decide to save the breadcrumb. Whats the best way to do it? I used http://www.djangosnippets.org/snippets/1694/ and have crumb = PickledObjectField().
Thats the method to calculate the attribute crumb()
def _breadcrumb(self):
breadcrumb = [ ]
x = self
while True:
x = x.father
try:
if hasattr(x, 'country'):
breadcrumb.append(x.country)
elif hasattr(x, 'region'):
breadcrumb.append(x.region)
elif hasattr(x, 'city'):
breadcrumb.append(x.city)
else:
break
except:
break
breadcrumb.reverse()
return breadcrumb
Thats my save-Method:
def save(self,*args, **kwargs):
# how can I access the father ob the object?
father = self.father # does obviously not work
father = kwargs['father'] # does not work either
# the breadcrumb gets calculated here
self.crumb = self._breadcrumb(father)
super(GeoObject, self).save(*args,**kwargs)
Please help me out. I am working on this for days now. Thank you.