I'm using a Vote model which can be generically linked to several different models (to allow you to vote on different things). In some of my code I use annotations to count up the total number of votes and the positive number of votes for a queryset. I've noticed that if I place a vote on a certain item, then the other types of models that have the same id also show that they've been voted on (both by the total votes and positive votes). If I look at the list of votes for the object that hasn't been voted on, it returns an empty list (but the annotation still says +1).
Here's some code:
class UserVote():
user = models.ForeignKey(User)
vote = models.SmallIntegerField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
class Model1():
votes = generic.GenericRelation(UserVote)
class Model2():
votes = generic.GenericRelation(UserVote)
And to reproduce error:
m1 = Model1()
m1.save()
m2 = Model2()
m2.save()
m1.votes.add(UserVote(user_id=1, vote=1))
Model2.objects.all().annotate(tot_votes=Count('votes__vote'))[0].tot_votes # Returns 1
Model2.objects.all().annotate(tot_votes=Count('votes__vote'))[0].votes.all() # Returns []
I'm not sure if this is a bug in the annotate code, or if I'm missing some extra bit that differentiates between votes on different generically related models. I'm running the trunk version of Django, btw.
Edit: It seems there's a bug open on it (I guess next time I'll search first) at http://code.djangoproject.com/ticket/10461 . I'll leave this open for posterity/maybe someone can figure out a workaround.