Re: Aggregates over reverse relationships?

2009-08-06 Thread Shai



On Aug 4, 2:09 am, Malcolm Tredinnick 
wrote:
> 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.
>

I am deeply surprised. I never thought reverse relationships worked so
smoothly.
The only problem with my version above is attempting to count the
player_set;
Count('player') works, as you said.

>
>         Team.objects.filter(player=None)
>

And this indeed works too, and surprises me too, because Team does not
have a
player field.

Well. You learn interesting Django tidbits every day.

Thanks,
Shai.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Aggregates over reverse relationships?

2009-08-03 Thread Malcolm Tredinnick

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 django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Aggregates over reverse relationships?

2009-08-03 Thread Shai Berger

The question about aggregates still stands -- however, if anybody is wondering 
about finding all childless parents, I figured a way:

On Monday 03 August 2009 17:45:18 Shai Berger wrote:
>
> 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)
>

team_players = Player.objects.all().values('team').query
Team.objects.exclude(pk__in=team_players)

Have fun,
Shai.





--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Aggregates over reverse relationships?

2009-08-03 Thread Shai Berger

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)

There may be some obvious way (which I'm currently missing) to solve the 
specific problem of finding the empty teams without such aggregates; however, 
they do make sense in general.

Is there a way?

Thanks,
Shai.




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---