This is exactly what select_related() does. The only gotcha is that
you have to start with the Answer model, rather than Question, but the
result is the same:
answers = Answer.objects.filter(question_id=1).select_related()
Now each answer object has a pre-fetched ‘question’ attribute, and
accessing it won’t hit the db again.
Related Contents:
- Select DISTINCT individual columns in django?
- Why does django’s prefetch_related() only work with all() and not filter()?
- How to filter empty or NULL names in a QuerySet?
- How to view corresponding SQL query of the Django ORM’s queryset?
- Custom QuerySet and Manager without breaking DRY?
- Django Aggregation: Summation of Multiplication of two fields
- Django – filtering on foreign key properties
- how to write a query to get find value in a json field in django
- How to limit queryset/the records to view in Django admin site?
- How to query Case-insensitive data in Django ORM?
- How can I combine two or more querysets in a Django view?
- Extending the User model with custom fields in Django
- When saving, how can you check if a field has changed?
- How do I filter query objects by date range in Django?
- Convert Django Model object to dict with all of the fields intact
- What’s the difference between select_related and prefetch_related in Django ORM?
- What’s the difference between django OneToOneField and ForeignKey?
- How to define two fields “unique” as couple
- Django: Get model from string?
- Dynamic File Path in Django
- Django: How can I protect against concurrent modification of database entries
- How to express a One-To-Many relationship in Django?
- Django – Circular model import issue
- How do I get Django Admin to delete files when I remove an object from the database/model?
- Get model’s fields in Django
- How to ‘bulk update’ with Django?
- Override default queryset in Django admin
- How can I restrict Django’s GenericForeignKey to a list of models?
- Django accessing ManyToMany fields from post_save signal
- In a django model custom save() method, how should you identify a new object?
- Using a UUID as a primary key in Django models (generic relations impact)
- Django – Simple custom template tag example
- How to create an object for a Django model with a many to many field?
- How can I have two foreign keys to the same model in Django?
- How do you serialize a model instance in Django?
- Tying in to Django Admin’s Model History
- Django orm get latest for each group
- Using Django auth UserAdmin for a custom user model
- Django ModelForm override widget
- Dropdown in Django Model
- django-orm case-insensitive order by
- Django Blob Model Field
- Django – Overriding the Model.create() method?
- Django Migrations Add Field with Default as Function of Model
- List field in model?
- django dynamically filtering with q objects
- Django: Validate file type of uploaded file
- How to assign currently logged in user as default value for a model field?
- Django Queryset with filtering on reverse foreign key
- Default image for ImageField in Django’s ORM