Django Queryset with filtering on reverse foreign key
I know this is very old question, but I am answering. As I think my answer can help others. I have changed the model a bit as follows. I have used Django 1.8.
class Make(models.Model):
name = models.CharField(max_length=200)
class MakeContent(models.Model):
make = models.ForeignKey(Make, related_name='makecontent')
published = models.BooleanField()
I have used the following queryset.
Make.objects.filter(makecontent__published=True)
Hope it will help.
Yes, I think you want
make = Make.objects.get(pk=1)
make.make_content_set.filter(published=True)
or maybe
make_ids = MakeContent.objects.filter(published=True).values_list('make_id', flat=True)
makes = Make.objects.filter(id__in=make_ids)