Django Admin (Registering Models, Customizing Admin Site)

Search for a command to run...

You can add search field using search_fields attribute on your admin.ModelAdmin class. Full code will look like this
class InformationAdmin(admin.ModelAdmin):
list_filter = ['date_of_birth']
search_fields = ['name']

This series will cover the core concepts on web application development using Django, from basic installation to user authentications and some core concepts. Status: 9/14 published ✅
function() based views in Django
Nothing serious, just experimenting on Django Framework

Working with simple media uploads and serving these media files

Using static files like JavaScript, CSS, images with Django

Class() based views in Django

function() based views in Django

Django under the hood provides an app called Admin a.k.a Django Admin, which provides Admin UI interface/web page for your Django Project. This admin interface can automatically generate CRUD views/ (form/tables) for your models with very minimal setup i.e (Registering your models to admin)
This Admin app is usually said to be one of the most powerful parts of Django
Let's deep to use Django Admin on our projects
Creating a test Model named Information
# inside your models.py file
from django.db import models
class Information(models.Model):
name = models.CharField(max_length=100)
date_of_birth = models.DateField()
salary = models.IntegerField()
Don't forget to create migrations and then migrate
Registering your models to Django Admin
admin.py file that exists in your app (when you create django app through manage.py startapp [app] command, you will automatically get this file along other files like models.py that we have been using)To let Django Admin work for our models, we simply register our model class on this admin.py file, by using admin.site.register()
from django.contrib import admin
from .models import Information # import your model
admin.site.register(Information) # actual registration
Create Superuser to login to Django Admin
User app and authentication process, but we will leave that for another tutorialpython manage.py createsuperuser commandUser table with attributes is_staff and is_superuser set to True. This, attributes are the table columnsis_staff or is_superuser set to True for authorizationWhich means, anyone, any user on our project can initiate login to the admin site but won't get access since other users is_staff and is_superuser are set to False
-> python manage.py createsuperuser
Username (leave blank to use 'djangotherightway'): admin
Email address: admin@example.com
Password:
Password (again):
Superuser created successfully.
login to Django Admin Site
Register Admin urls to our urls.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
/admin/ to something unguessable, so as to prevent users from bruteforcing this page or accessing this page. In production environment, i usually disable this url pathBy default, when creating a project using
django-admin startproject [project]command, Django will automatically do this for us
Run your Django server using python manage.py runserver and navigate to /admin/

Now, login using the credentials we just created on Step-3, and you will be able to see the model we registered on Step-2

You can now, perform CRUD operations to your models, like add a new object/row, view all objects/rows, update and delete
You may see that
GroupsandUsersmodels are already registered on Django admin through we didn't do it manually. This is because thisAuthapp has already registered those models on itsadmin.pyfile. You can see the code on github here
Django admin site is super customizable, but we will see some basics so that you can get some idea about its customizations.
Customizing Admin view for models can be done through admin.ModelAdmin class. We basically create a class using/subclassing admin.ModelAdmin and then tell Django admin to use this custom ModelAdmin while registering a model

This table view doesn't look nice, so lets add some columns/fields on this table view using list_display attributes on admin.ModelAdmin
# on your admin.py
from django.contrib import admin
from .models import Information
class InformationAdmin(admin.ModelAdmin):
list_display = ('name', 'date_of_birth', 'salary')
admin.site.register(Information, InformationAdmin)

Let's create up age field obtained from date_of_birth and display it on the list display table fields
from django.contrib import admin
from .models import Information
class InformationAdmin(admin.ModelAdmin):
list_display = ('name', 'date_of_birth', 'salary', 'age')
@admin.display()
def age(self, obj):
# this is rough implementation of finding
# out age from date of birth, so as to keep
# this example as easy as possible
from datetime import datetime
current_year = datetime.today().year
born_year = obj.date_of_birth.year
return current_year - born_year

We can use list_filters attribute to set filter fields
from django.contrib import admin
from .models import Information
class InformationAdmin(admin.ModelAdmin):
list_filters=['date_of_birth']
admin.site.register(Information,InformationAdmin)

These were all basics things that we can do with Django Admin interface, but there are much more advance customizations available to use.
If i should write more on advanced Customization, let me know in comments.