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
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
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:
Thanks Williams.:)
My problem is solved.
-Ram