Django Extensions – Installing and Implementing Extensions

Django Extensions

In this article, we will explore how Django extensions can enhance the default functionality provided by Django, a popular Python framework.

Django extensions are a package of custom extensions that developers can add to their Django projects to extend the default capabilities. They include admin extensions, command extensions, debugger tags, field extensions, model extensions, and validators. These extensions provide additional functionalities like debugging Django templates, enhancing data fields, streamlining Django models, and ensuring data integrity

Exploring the Power of Django Extensions

The Django extensions package is open-source, and its repository is available on GitHub for anyone to download and contribute. Here are some of the key extensions available in the Django extensions package that can be added to your project:

  • Admin extensions
  • Command extensions
  • Debugger tags
  • Field Extensions
  • Model Extensions
  • Validators
  • Powershell

A detailed analysis of these extensions is provided in the table below. This information is based on the official Django extensions documentation.

Supercharging the Admin Interface with Extensions

Some of the important Extensions are:

ForeignKeyAutocompleteAdminEnables the admin app to show ForeignKey fields with a search input field.
and ForeignKeyAutocompleteTabularInline
Similar to AutocompleteAd, these two classes enable search input field for Foreignkey fields in the AdminInline Classes
Admin Extensions

Command Extensions: Power at Your Fingertips

Some of the most used command extensions are:

Shell_plusEnhanced version of shell; has all the models autoloaded, hence we can work with ORM right away.
Create_template_tagGenerates the Template Tag directory structure within the specified application
generate_passwordIt generates a new password that can be used as a user password.
export_emailsExports email addresses in one of the following formats – Google, Outlook, LinkedIn, and VCard
graph_modelsIt creates a GraphViz dot file for apps. It combines the models of apps (passed) into a single model. The output is usually directed to a dot file.
run_scriptRuns a script in Django
sql_createGenerates the SQL to create a database, as specified in
Command Extensions

We will see more about the shell – shell_plus in the later sections.

Debugger Tags: Simplifying Django Template Debugging

With Debugger Tags, we can debug Django templates easily. These template tags provide three filters – IPDB, PDB or WDB

The syntax is:

{% load debugger_tags %}

{% for object in objects %}
{{object | ipdb/pdb/wdb }}
{% endfor %}
IPDBIt is an Ipython debugger tool used with Ipython to debug the model object (with which it is used)
PDBIt is the Normal Python Debugger to debug the model Object
WDBIt is a Web Debugger tool used to debug the python-based applications via the browser itself.
Debugger Tags

Note: To use the filters, we need to load the debugger tags at the top of the template files

{% load debugger_tags %}

Field Extensions: Enhancing Data Fields in Django

Some of the useful field extensions are:

RandomCharFieldCreates a unique random character with the specified length
DateTimeFieldDateTimeField automatically set its date when the object is first saved to the database
JSONFieldThis Text Field neatly serializes/deserializes JSON objects
Field Extensions

Model Extensions: Streamlining Your Django Models

These are the important Model extensions:

ActivatorModelThis Abstract Base Class provides a status, activate_date and deactivate_date
TitleDescriptionModelThis Abstract Base Class model provides title field, which is a non-nullable CharField with maximum length –255, and a nullable description field.
Modle Extensions

Validators: Ensuring Data Integrity in Django

Some of the important Validators provided by extensions are:

NoControlCharacterValidatorThis attribute ensures that the Control Characters like new lines or tabs are not allowed.
NoWhitespaceValidatorThis attribute ensures that the leading and trailing whitespace is not allowed.
HexValidatorEnsures that the string a valid hex string. Can Optionally also specify length, min_length and max_length parameters

How to Install Django Extensions

We can install the extensions using pip command itself. Run the code

pip install django-extensions

We need to mention the extensions in our as well. You just need to add ‘django_extensions’ to your INSTALLED_APPS in the configuration file.


With that, the extensions are now ready for use in your project.

Implementing Django Extensions in Your Project

Next, let’s delve into how to use these important extensions in our Django project. The code snippets provided are written in Python.

Mastering the Implementation of Admin Extensions in Django

Consider the BlogModel we built in a previous article with the following fields in the file

from django.db import models

class BlogModel(models.Model):
    id = models.IntegerField(primary_key=True)
    blog_title = models.CharField(max_length=20)
    blog = models.TextField()

    def __str__(self):
        return f"Blog: {self.blog_title}"

Also, consider a CommentModel which contains the comments for a specific blog from the BlogModel Table. Add the Comment Model below the BlogModel in the file:

class CommentModel(models.Model):
    your_name = models.CharField(max_length=20)
    comment_text = models.TextField()
    blog = models.ForeignKey('BlogModel', on_delete=models.CASCADE)
    def __str__(self):
        return f"Comment by Name: {self.your_name}"

Note that the blog field is the FK connecting the BlogModel. Now in the file, we will apply admin extensions to the Comment Model template.

from .models import *
from django_extensions.admin import ForeignKeyAutocompleteAdmin
class BookModelAdmin(ForeignKeyAutocompleteAdmin):

    related_search_fields = {

    fields =('your_name','comment_text','blog')

Do check out the Admin Template article for more information about Admin templates

Now run the server and go to the admin site

Admin Extensions
Admin Extensions

See how the search option appears for the FK blog field.

Leveraging shell_plus with IPython for Enhanced Django Shell Experience

The shell_plus command is an enhanced version of the shell, which is part of the Django framework. To run shell_plus with Ipython, we first need to install it.

Ipython is an interactive shell built for Python. It makes the shell environment efficient and easy to use. It provides useful environment tools and commands which we can use.

To install IPython, run the command:

pip install ipython

Now run the shell_plus along with ipython using the command below

python shell_plus --ipython
Shell Plus With Ipython
Shell Plus With Ipython

See, all the models present in the project are auto-loaded in the shell.

Some other important extensions

We will now see some of the most used extensions here

Utilizing RandomCharField Extension for Unique Random Characters

The RandomCharField is a feature that creates a unique random string of the specified length. You can add this Field in your Model using the command below:

from django.db import models
from django_extensions.db.fields import RandomCharField

class ModelName(models.Model):
    Field1 = RandomCharField(length=12, unique =True, include_punctuation=True)

Implementing Validators for Robust Data Validation

Validators are crucial, ensuring data integrity in your Django applications. The syntax for this in is:

from django_extensions.validators import HexValidator

class ModelName(models.Model):
    Field1 = models.CharField(max_length=80, validators=[HexValidator(length=64)])


That’s all for now! We’ve covered a lot about Django Extensions. If you encounter any issues, their community on GitHub is a great source of support. Remember to check the official documentation for the most up-to-date information. Keep coding in Python and enhancing your projects! Do check out our other articles.