I need to annotate each query in queryset using a model method:
class myModel(models.Model):
...
def myModel_foo(self):
....
return myModel_foo
I need something like .annotate(myModel_foo=myModel.myModel_foo()). The problem is myModel_foo() requires self. I tried iterating over queryset and using query.annotate(myModel_foo=myModel.myModel_foo(self)), but i got object has no attribute 'annotate' error. What's the correct approach?
UPDATE
OK, the idea is this: i have two models with single to one relation.
class myModel1(models.Model):
fk = ForeignKey(myModel2)
status = ChoiceField
class myModel2(models.Model):
def get_status(self):
# query all objects from myModel1, get status of the last one and
# return it
return get_status
Then I want to send a queryset with status included in it via ajax call.
qs = MyModel2.objects.all()
qs_json = serializers.serialize('json', qs)
return HttpResponse(qs_json, content_type='application/json')
How can i access it in template then in a way like qs_json[0].get_status?
Sum('field')for example.