Thursday, September 1, 2011

Re: queryset which returns the same result several times

On Thu, Sep 1, 2011 at 1:39 PM, Julien Castets <castets.j@gmail.com> wrote:
> hello www,
>
> This is the model:
>
> from django.contrib.auth.models import User
>
> class Task(models.Model):
>  creator = models.ForeignKey(User)
>  assigned_to = models.ManyToManyField(User)
>
> And there are four users: bob, roger, dan and joe
>
> I want to get all the tasks where the current user is the creator or
> where he's assigned to. I tried this request:
>
> requests = Request.objects.filter(Q(assigned_to=request.user) |
> Q(by=request.user))
> or this one, which is the same :
> requests = Request.objects.filter(Q(assigned_to__in=[request.user]) |
> Q(by=request.user))
>
> Problem:
>
> I have a task, bob is the creator, roger, dan and joe are assigned to it.
> The request returns the same result three times (i.e. [Task(pk=1),
> Task(pk=1), Task(pk=1)]
>
> Do you have an idea about why? And how can I get only unique results?
>

https://docs.djangoproject.com/en/1.3/ref/models/querysets/#distinct

Cheers

Tom

--
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.

No comments:

Post a Comment