20👍
For ForeignKey, you can use select_related():
Comment.objects.select_related('video').all()
It will generate one query only, gathering comments for you as well as videos.
For something more complex (such as M2M), you need an external app such as unjoinify to make optimizations but it uses SQL queries to then put them back in objects.
If you are unconfortable with this (I am), you have some alternatives:
- django-queryset-transform: not a full solution, but helps
- django-batch-select: roughtly a
select_related
that works with M2M and reverse relations.
4👍
What you need to do is use the select_related on the Comment.
Lets say you need to find the all the videos whose title starts with ‘The’ and the comments related to it
comments = Comment.objects.filter(video__title__starts_with='The')
.select_related('video').all()
This will load all the comments and the appropriate Video object for that comment. You will still need to pivot over the Video to iterate over the videos. Use the python itertools.groupby function to do that pivoting in memory.
- Python multiple inheritance function overriding and ListView in django
- Django migration relation does not exist
- TimeField format in Django template
- Get_list_or_404 ordering in django
- Django database delete specific number of entries
- "Apps aren't loaded yet" when trying to run pytest-django
- Python Django custom template tags register.assignment_tag not working
-1👍
As I don’t have enough reputations to comment, (lost all my previous reputation out of a joke of a fiend ahah). I will just advise people reading the solutions to be careful when using select_related
because it loads all your data in the memory, and I you don’t have enough memory, it can break your app, so be careful using it
- Testing a session variable
- Error installing mysqlclient for python on Ubuntu 18.04
- Configure Django-rest
- How to install pygments on Ubuntu?
- Django querysets + memcached: best practices