tags:

views:

29

answers:

1

Basically I want to do something similar to annotating a queryset but with a call on a function in the model attached to the response.

Currently I have something like:

objs = WebSvc.objects.all().order_by('content_type', 'id')
for o in objs:
    o.state = o.cast().get_state()

where get_state() is a function in the model that calls an external web service. I don't want to go down the road of caching the values. I was just hoping for a more succinct way of doing this.

+2  A: 

One way to do this, using python properties:

class WebSvc(models.Model):
    ...

    def _get_state():
        return self.cast().get_state()

    state = property(_get_state)

Advantages: will only run when the property is needed.

Possible disadvantage: when you call the property multiple times, the web service will be called anew (can be required behaviour but I doubt it). You can cache using memoization.

Other way, just do it by overriding init:

class WebSvc(models.Model):
    ...
    def __init__(*args, **kwargs):
        super(WebSvc, self).__init__(*args,**kwargs)
        self.state = self.caste().get_state()

Advantages: Will only be computed once per instance without need for memoization.

Possible disadvantage: will be calculated for each instantiated object.

In most typical django cases however, you will only run once over properties of an object and you will probably not instantiate object where you won't use the .state property on. So in these cases the approaches are more or less similar in 'performance'.

KillianDS