List Info

Thread: Restricting q QuerySet based on ManyToManyField




Restricting q QuerySet based on ManyToManyField
user name
2007-01-28 10:19:50
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


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


Re: Restricting q QuerySet based on ManyToManyField
user name
2007-01-29 07:52:00
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:
> 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


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


Re: Restricting q QuerySet based on ManyToManyField
user name
2007-01-29 21:36:34
> 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/mo
dels/many_to_many/> starting
> with block "# We can perform kwarg queries across
m2m relationships".

Thanks Williams.
My problem is solved.

-Ram


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


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )