Django Get Last Record Example

Hi Dev,
In this short tutorial we will cover an django get last record. you can understand a concept of django get last record in queryset. This article goes in detailed on how to get last inserted record in django. if you have question about django get last record id then I will give simple example with solution. Here, Creating a basic example of django get last record by id.
In this tuts, There are several ways to django get last record. we will use latest() and last() functions to get last record from database. so let's see the below examples.
let's see below simple example with output:
Example 1:In this example returns the latest User in the table, according to the id field:
views.pyfrom django.shortcuts import render from django.http import HttpResponse from django.contrib.auth.models import User def index(request): user = User.objects.latest('id') print(user) return HttpResponse('')Output
<User: bhavesh>Example 2:
In this example returns the lastUser in the table, according to the id field:
views.pyfrom django.shortcuts import render from django.http import HttpResponse from django.contrib.auth.models import User def index(request): user = User.objects.last() print(user) return HttpResponse('')Output
<User: bhavesh>
I Hope It will help you....