Country -> Team -> Player
If there is such a relation, there are times when you want to get the Team list and change the order of Players.
In such a case, you can use the Prefetch object.
views.py
from django.db.models import Prefetch
def get_queryset(self):
queryset = Team.objects.filter(country="japan").prefetch_related(
Prefetch(Player, queryset=Player.objects.order_by('name')))
return queryset
Set the Prefetch object in the contents of prefetch_related
.
QuerySet API reference |Django documentation| Django https://docs.djangoproject.com/ja/2.2/ref/models/querysets/#prefetch-objects
Recommended Posts