20👍
Django doesn’t support the select_related()
method for reverse foreign key lookups, so the best you can do without leaving Python is two database queries. The first is to grab all the Makes
that contain MakeContents
where published = True
, and the second is to grab all the MakeContents
where published = True
. You then have to loop through and arrange the data how you want it. Here’s a good article about how to do this:
http://blog.roseman.org.uk/2010/01/11/django-patterns-part-2-efficient-reverse-lookups/
75👍
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)
- [Django]-No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model
- [Django]-Django-taggit – how do I display the tags related to each record
- [Django]-How to get value from form field in django framework?
53👍
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)
You should use distinct()
to avoid the duplicate result.
Make.objects.filter(makecontent__published=True).distinct()
I hope it will help.
- [Django]-Django – what is the difference between render(), render_to_response() and direct_to_template()?
- [Django]-Django models avoid duplicates
- [Django]-How to tell if a task has already been queued in django-celery?
19👍
Let me translate Spike’s worded answer into codes for future viewers. Please note that each ‘Make’ can have zero to multiple ‘MakeContent’
If the asker means to query ‘Make’ with AT LEAST ONE ‘MakeContent’ whose published=True, then Jason Christa’s 2nd snippet answers the question.
The snippet is equivalent to
makes = Make.objects.select_related().filter(makecontent__published=True).distinct()
But if the asker means to query ‘Make’ with ALL ‘MakeContent’ whose published=True, then following the ‘makes’ above,
import operator
make_ids = [m.id for m in makes if
reduce(operator.and_, [c.published for c in m.makecontent_set.all()] )
]
makes_query = Make.objects.filter(id__in=make_ids)
contains the desired query.
- [Django]-How to make an auto-filled and auto-incrementing field in django admin
- [Django]-How do I run tests against a Django data migration?
- [Django]-What is the path that Django uses for locating and loading templates?
3👍
One more time, it is not clear what was the question, but if it was desired that all related MakeContent objects must have been published this can work:
Make.objects.exclude(MakeContent_set__published=False)
And if at least one of them (as it was in other answers):
Make.objects.filter(MakeContent_set__published=True)
- [Django]-Django-debug-toolbar not showing up
- [Django]-How do I deploy Django on AWS?
- [Django]-Removing 'Sites' from Django admin page