[PYTHON] Change the order of PostgreSQL on Heroku

background

Regarding Django. When arranging the posts in the database in the order of creation (ID order), MySQL that is included by default is stored in the ID order in the local environment, so there was no particular difficulty.

However, after deploying with Heroku, MySQL cannot be used, so PostgreSQL will be used. The order of data in PostgreSQL is not the order of ID by default (update order? I still don't know how it is arranged), so you need to set it yourself.

counter-measure

I thought about going inside the database and setting the ORDER_BY clause there (probably this is the royal road), but I'm not used to it, so I added a code on Django's views.py, which is easier. By inserting ```ordering = ['id'] `` `, there will be no problem on the display.

views.py


class DataList(ListView):
    template_name = "list.html"
    model = DataModel
    ordering = ['-id']

About ordering

The character string enclosed in `''``` can write the name of the database field. If you want to sort in descending order, add `'-'to the beginning as an option. You can use"? "``` To make the order random.

reference

What to do if Django sorts out of order Django v1.0 documentation model Meta options (https://djangoproject.jp/doc/ja/1.0/ref/models/options.html)

Recommended Posts

Change the order of PostgreSQL on Heroku
Change the resolution of Ubuntu running on VirtualBox
Change the theme of Jupyter
Change the style of matplotlib
Change the Python version of Homebrew
Deploy the Flask app on Heroku
Change the suffix of django-filter / DateFromToRangeFilter
Deploy the Flask app on heroku
Change the length of Python csv strings
Script to change the description of fasta
Arbitrarily rearrange the column order of Pandas.DataFrame
Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
Post the subject of Gmail on twitter
Change the destination batfish server of pybatfish
Do not change the order of columns when concatenating pandas data frames.
Let's change the color scheme of iTerm2 automatically depending on the time zone
A memo of a tutorial on running python on heroku
Redis on Heroku
Change the font size of the legend in df.plot
Change the Key of Object on S3 from normal date format to Hive format
The behavior of signal () depends on the compile options
In Python, change the behavior of the method depending on how it is called
[2020July] Check the UDID of the iPad on Linux
Use the latest version of PyCharm on Ubuntu
Change the color of Fabric errors and warnings
Change the decimal point of logging from, to.
Calculate the probability of outliers on a boxplot
At the time of python update on ubuntu
[AWS S3] Confirmation of the existence of folders on S3
Posted the number of new corona positives in Tokyo to Slack (deployed on Heroku)
I tried to make it easy to change the setting of authenticated Proxy on Jupyter
Install the latest version of CMake on Ubuntu 18.04.4 LTS
[Touch Designer] Change the appearance color of Button COMP
Maybe I overestimated the impact of ShellShock on CGI
Try to estimate the number of likes on Twitter
Drawing on Jupyter using the plot function of pandas
About the uncluttered arrangement in the import order of flake8
[Django] Change the Default IP address of the runserver command
Tweet the triple forecast of the boat race on Twitter
Difference in results depending on the argument of multiprocess.Process
Scroll Japanese on the LED of RaspberryPi Sense HAT
Miscellaneous notes about deploying the django app on Heroku
A Study on Visualization of the Scope of Prediction Models
Create a shape on the trajectory of an object
Announcing the availability of Java 11 LTS on Amazon Linux 2
[Python] Let's change the URL of the Django administrator site
00. Reverse order of strings
The beginning of cif2cell
I want to change the symbolic link destination of / lib64 from / usr / lib64 to / my-lib64 on CentOS
The meaning of self
the zen of Python
The return value of len or unichr may change depending on whether it is UCS-2 or UCS-4.
The story of sys.path.append ()
Revenge of the Types: Revenge of types
Freely customize the model. Change the variable name and field name of the model of the Django / Django Rest Framework app ~ How to reflect the deployment on heroku Memo
Notes on the version of CUDA, cuDNN where tensorflow-gpu worked
[Python] Change the Cache-Control of the object uploaded to Cloud Storage
A note on the default behavior of collate_fn in PyTorch
A memorandum of stumbling on my personal HEROKU & Python (Flask)
About the camera change event of Google Maps Android API