[PYTHON] Get the first element of queryset

A small story about Django.

https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.first

It seems that first (and last) have been added to the queryset since about Django 1.6, and I now know that it can be conveniently used to take the first item ...

try:
  p = Article.objects.order_by('title', 'pub_date')[0]
except IndexError:
  p = None
p = Article.objects.order_by('title', 'pub_date').first()

Well convenient. It seems that Django 1.8 has also been released, so I have to check for new features.

Recommended Posts

Get the first element of queryset
Get the number of digits
Get the number of views of Qiita
First Python 3 ~ The beginning of repetition ~
Get the attributes of an object
Get the number of occurrences for each element in the list
Get the number of Youtube subscribers
Get the index of each element of the confusion matrix in Python
Note: Get the first and last items of Python OrderedDict non-destructively
[Python] How to get the first and last days of the month
Get the column list & data list of CASTable
Get the minutes of the Diet via API
Take the logarithm of the nonzero element of scipy.sparse
I can't get the element in Selenium!
Get the value of the middle layer of NN
Get the last day of the specified month
[Python] Get the character code of the file
Get the filename of a directory (glob)
[PowerShell] Get the reading of the character string
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
Get only the address part of NIC (eth0)
To get the path of the currently running python.exe
[Python] Get the day of the week (English & Japanese)
Get the update date of the Python memo file.
Note: How to get the last day of the month with python (added the first day of the month)
Get the last element of the array by splitting the string in Python and PHP
Get the title of yahoo news and analyze sentiment
The beginning of cif2cell
Get the GNOME version
[Python] Get the official file path of the shortcut file (.lnk)
The meaning of self
[Python] Get the text of the law from the e-GOV Law API
Get the image of "Suzu Hirose" by Google image search.
Get the absolute path of the script you are running
[python] Get the list of classes defined in the module
I followed the implementation of the du command (first half)
Since there are many earthquakes, get the history of earthquakes
Get the return code of the Python script from bat
[C language] [Linux] Get the value of environment variable
The story of sys.path.append ()
Get to know the feelings of gradient boosting trees
Get the size (number of elements) of UnionFind in Python
Try to get the contents of Word with Golang
[Python] Get the list of ExifTags names of Pillow library
Record of the first machine learning challenge with Keras
Get the MIME Type
[Django 2.2] Sort and get the value of the relation destination
Runner-up in the first hackathon of the Zukoke rookie trio!
Get the operation status of JR West with Python
Script to get the expiration date of the SSL certificate
Why is the first argument of [Python] Class self?
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
Revenge of the Types: Revenge of types
Get the output value of the command (as received by xargs)
Try to get the function list of Python> os package
I tried to get the location information of Odakyu Bus
Get the number of specific elements in a python list
Summary of stumbling blocks in Django for the first time