How to Display a Database Table with the Latest Entries Showing First with Python in Django 


               


How to Display a Database Table with the Latest Entries Showing First with Python in Django



Python


In this article, we show how to display a database table with the latest entries showing first with Python in Django.

So how do we go about doing this?

So the best way to do is through a column that's in every database table, the id table.

Every database table should have an id column that serves as the primary key for the database table.

Every id is unique and most importantly, the latest entries have the greatest ids. The last entries in the table have the highest id values. Therefore, we can use the id column to display the database table with the latest entries showing first.

So let's say we have a database called Comment. It represents user comments. We want the latest comments to show first. This is how most websites do it and this is what makes the most sense. The latest comments should be shown at the top.

Again, the database table is Comment.


views.py File

order_by Method

So let's now go to the views.py file.

In this views.py file, we're going to import the Comment database table.

We're then going to use the order_by function to sort the id column in descending order using the following code below.



So in this code, we get the objects of the Comment database table and order them in descending order by the id column.

It's in descending order because there is a - (minus) symbol in front of id.


Custom Function

Just to show you another way, we do this same thing below in a custom function.

So let's now go into the Python code.



So we have to import render, since we're rendering a template file.

We then have to import the Comment database table from the models.py file.

We then create a view-based function, called comments.

Inside of this comments function, we retrieve all objects from the Comment database table and store it in the variable, allcomments.

We then create another variable, sortedcomemnts, which will have this list sorted with the id from highest to lowest. We use the sorted() function to sort the list. All we have to do is use a lambda function to sort the table according to any column. We sort the table according to the id column. And we put in, reverse=True, because we're sorting according to the id column from highest to lowest.

The lambda function is the easiest way to achieve the sorting, because you don't have to explicitly create a function.

We then pass in the list of comments sorted by id.

We then render the template file, blogpost1.html


template File

We then going to print out all rows of the database table, with the table showing the latest comments first.

The contents of this template file is shown below.



So basically we just print out the user comments, now with the last comments, or latest entries, appearing first.

So this is how we can show the latest entries of a database table first with Python in Django.


Related Resources

How to Create Dynamic URLs in Django

How to Create a Video Uploader with Python in Django

How to Create an Image Uploader with Python in Django



HTML Comment Box is loading comments...