Wednesday, March 23, 2011

Re: Exclude results with ManyToManyField

On Mar 23, 2011, at 12:19 PM, Julien Castets wrote:

> Hello,
>
> from django.contrib.auth.models import User
>
> class Team(models.Model):
> users = models.ManyToManyField(User)
>
>
> I would like to retrieve every User in my database who does not belong
> to a team.

Left outer join is your friend ....

User.objects.filter(team__users__isnull = True)


> I'm facing to this problem for several hours, and I found a *very*
> crappy solution :
>
> def _get_all_users_in_teams():
> for team in Team.objects.all():
> for user in team.users.all():
> yield user.pk
>
> CHOICES = User.objects.all().exclude(pk__in=[id for id in
> _get_all_users_in_teams()])
>
> Is there something better?
> I'm on django 1.2.4
>
> Regards,
> --
> J.
>
> --
> 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+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate