Re: Restricting q QuerySet based on ManyToManyField

6 views
Skip to first unread message

Ramashish Baranwal

unread,
Jan 28, 2007, 12:29:03 PM1/28/07
to Django users
On 1/28/07, Ramashish Baranwal <ramashi...@gmail.com> wrote:
> Hi,
>
> I would like to restrict a QuerySet based on its ManyToManyField. In
> particular, based on what the ManyToManyField contains. Consider this
> trivial and a bit non-sense example-
>
> class User:
> name = models.CharField(maxlength=24)
> related = models.ManyToManyField('self', symmetric=False)
>
> I want to get Users who are related to other users whose names start
> with 'A'. e.g.
>
> pplA = User.objects.filter(name__starts_with='A')
>
> # The below doesn't result in what I expect.
> #relatedA = User.objects.filter(related__in=ppl)
>
> I am looking for a clause that can help me accomplish this but ain't
> able to find in docs.
>

There have been a couple of typos in my earlier Post. Sorry for that,
the correctected version is-

I would like to restrict a QuerySet based on its ManyToManyField. In
particular, based on what the ManyToManyField contains. Consider this
trivial and a bit non-sense example-

class User:
name = models.CharField(maxlength=24)
related = models.ManyToManyField('self', symmetric=False)

class User(models.Model):
name = models.CharField(maxlength=24)
related = models.ManyToManyField('self', symmetric=False)

I want to get Users who are related to other users whose names start
with 'A'. e.g.

pplA = User.objects.filter(name__starts_with='A')

# The below is for illustration only, it doesn't do what I expect.
#relatedA = User.objects.filter(related__in=pplA)

I am looking for a clause that can help me accomplish this but ain't
able to find in docs. Please note that the users in relatedA might
also be related to other users not in pplA too, all I want is they
should atleast be related to one user in pplA.

Thanks,
-Ram

Ramashish Baranwal

unread,
Jan 28, 2007, 11:19:50 AM1/28/07
to Django users
Hi,

I would like to restrict a QuerySet based on its ManyToManyField. In
particular, based on what the ManyToManyField contains. Consider this
trivial and a bit non-sense example-

class User:
name = models.CharField(maxlength=24)
related = models.ManyToManyField('self', symmetric=False)

I want to get Users who are related to other users whose names start
with 'A'. e.g.

pplA = User.objects.filter(name__starts_with='A')

# The below doesn't result in what I expect.
#relatedA = User.objects.filter(related__in=ppl)

I am looking for a clause that can help me accomplish this but ain't
able to find in docs.

Thanks in advance,
-Ram

hoa...@gmail.com

unread,
Jan 29, 2007, 8:52:00 AM1/29/07
to Django users
Ram-

I believe that

User.objects.filter(related__name__starts_with='A')

would get you what you're looking for, per the example at <http://
www.djangoproject.com/documentation/models/many_to_many/> starting
with block "# We can perform kwarg queries across m2m relationships".

On Jan 28, 11:19 am, "Ramashish Baranwal" <ramashish.li...@gmail.com>
wrote:

Ramashish Baranwal

unread,
Jan 29, 2007, 10:36:34 PM1/29/07
to Django users
> Ram-
>
> I believe that
>
> User.objects.filter(related__name__starts_with='A')
>
> would get you what you're looking for, per the example at <http://www.djangoproject.com/documentation/models/many_to_many/> starting

> with block "# We can perform kwarg queries across m2m relationships".

Thanks Williams.:)
My problem is solved.

-Ram

Reply all
Reply to author
Forward
0 new messages