[PYTHON] [Django 2.2] Sort and get the value of the relation destination

How to sort the value of the relation destination

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.

Official Doc

QuerySet API reference |Django documentation| Django https://docs.djangoproject.com/ja/2.2/ref/models/querysets/#prefetch-objects

Recommended Posts

[Django 2.2] Sort and get the value of the relation destination
Get the value from the [Django] Form
Get and set the value of the dropdown menu using Python and Selenium
Get the title of yahoo news and analyze sentiment
relation of the Fibonacci number series and the Golden ratio
[C language] [Linux] Get the value of environment variable
Get the URL of the HTTP redirect destination in Python
Get the number of digits
Differentiation of sort and generalization of sort
Relationship between Bound / Unbound and initial value of Django2 Form
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller
How to get the pixel value of the point from the satellite image by specifying the latitude and longitude
Flow of getting the result of asynchronous processing using Django and Celery
[Python] How to get the first and last days of the month
About the return value of pthread_mutex_init ()
Get the attributes of an object
About the return value of the histogram.
Get the first element of queryset
Get the number of Youtube subscribers
The meaning of ".object" in Django
Get the title and delivery date of Yahoo! News in Python
Get the query string (query string) in Django
Use Cloud Dataflow to dynamically change the destination according to the value of the data and save it in GCS
I measured 6 methods to get the index of the maximum value (minimum value) of the list
Read the graph image with OpenCV and get the coordinates of the final point of the graph
Get news from three major mobile companies using Django and the News API
Get and estimate the shape of the head using Dlib and OpenCV with python
Get the return value of an external shell script (ls) with python3
Consider the description of Dockerfile (Django + MySQL②)
Get the client's IP address in Django
I tried the asynchronous server of Django 3.0
This and that of the inclusion notation.
Understand the benefits of the Django Rest Framework
How to check the version of Django
Get the column list & data list of CASTable
Review the concept and terminology of regression
Get the minutes of the Diet via API
The story of trying deep3d and losing
Get the last day of the specified month
[Python] Get the character code of the file
Get the filename of a directory (glob)
Change the destination batfish server of pybatfish
Get the address from latitude and longitude
[PowerShell] Get the reading of the character string
Get only the text from the Django form.
Consider the description of docker-compose.yml (Django + MySQL ③)
Make the default value of the argument immutable
Udemy "Master the basics of Django and create three apps! The results of doing it"
Fill the missing value (null) of DataFrame with the values before and after with pyspark
How to count the number of elements in Django and output to a template
Define a division value in Django and easily reflect it on the screen
Get the stock price of a Japanese company with Python and make a graph
Get the last element of the array by splitting the string in Python and PHP
Get the contents of git diff from python
Specify the Django http status code and return.
The easiest way to get started with Django
[Python] Sort the list of pathlib.Path in natural sort
Summary of the differences between PHP and Python
Full understanding of the concepts of Bellman-Ford and Dijkstra
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News