Re: Query spanning two foreign keys
On Nov 15, 7:55 pm, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote: > This should do the job: > > User.objects.filter(posts__parent__pk=1).distinct() That does the trick. Thanks! --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Query spanning two foreign keys
On Sat, 2008-11-15 at 17:09 -0800, Will McCutchen wrote: > Hi all, > > I've got the following models (simplified for this example): > > from django.db import models > from django.contrib.auth.models import User > > class Chat(models.Model): > name = models.CharField(max_length=256) > > class Post(models.Model): > user = models.ForeignKey(User, related_name='posts') > parent = models.ForeignKey(Chat, related_name='posts') > > Can anyone tell me the best way to get a unique list of the users who > have contributed to a specific chat? I can do something like the > following, but I feel like there must be a more elegant way: > > >>> chat = Chat.objects.get(pk=1) > >>> user = set([post.user for post in chat.posts.all()]) > > Any suggestions? Do I need to resort to raw SQL? This should do the job: User.objects.filter(posts__parent__pk=1).distinct() Regards, Malcolm --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Query spanning two foreign keys
Hi all, I've got the following models (simplified for this example): from django.db import models from django.contrib.auth.models import User class Chat(models.Model): name = models.CharField(max_length=256) class Post(models.Model): user = models.ForeignKey(User, related_name='posts') parent = models.ForeignKey(Chat, related_name='posts') Can anyone tell me the best way to get a unique list of the users who have contributed to a specific chat? I can do something like the following, but I feel like there must be a more elegant way: >>> chat = Chat.objects.get(pk=1) >>> user = set([post.user for post in chat.posts.all()]) Any suggestions? Do I need to resort to raw SQL? Thanks, Will. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---