On Mon, 2009-08-03 at 17:45 +0300, Shai Berger wrote:
> Hi all,
>
> With the new aggregates in django 1.1, I wonder if there's an easy way to
> aggregate over reverse relationships. As an example, assume a simplistic
> representation of a set of teams:
>
> class Team(models.Model):
> name = models.CharField(max_length=100)
>
> class Player(moedls.Model):
> name = models.CharField(max_length=100)
> team = models.ForeignKey(Team)
>
> Now, I want to perform some operation on all the empty teams -- so I want to
> do something like
>
> Team.objects.annotate(num_players=Count('player_set')).filter(num_players__eq=0)
So what happened when you tried this? It looks like it should work and I
use similar sorts of queries with success in existing code.
> There may be some obvious way (which I'm currently missing) to solve the
> specific problem of finding the empty teams without such aggregates;
There is. :)
Team.objects.filter(player=None)
which is the same as
Team.objects.filter(player__isnull=True)
Regards,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---