Updated May 22, 2023
Introduction to Django Extensions
We know that Django provides different types of services to the user, which extends one of the services that Django provides. Django extensions are nothing but the collection of all custom extensions for the framework; Django extensions include different types of management commands and provide the additional field for database, admin extension, and many more. In the Django project, we have various additional commands that can be used per our requirements. For example, suppose we need to enable the admin app to see ForeignKey or any other; then, we need to add the extension.
What are Django Extensions?
Essentially, Django expansions are only the assortment of all traditional augmentations for the framework; Django expansions incorporate various kinds of administration orders; it likewise gives the extra field to the information base, administrator augmentation, and more. Django provides different types of extensions.
Admin Extensions
ForeignKeyAutocompleteAdmin: By using this extension, we can enable the admin app to see the foreignkey whenever we require it. So we need to render this extension with jQuery. With the help of this extension, we overcome the security issues. Autocompletion doesn’t check consent or the mentioned models on the autocompletion view. This can be utilized by clients with admittance to the administrator to uncover information from different models. So if it’s not too much trouble, know and be cautious while utilizing ForeignKeyAutocompleteAdmin. The ongoing adaptation of the ForeignKeyAutocompleteAdmin definitely dislikes late Django forms.
We firmly recommend that the project utilize this expansion refresh to Django 2.0 and the local autocomplete_fields.
Example:
Now let’s see the example of admin extension as follows:
Code:
from django.contrib import admin
from foo.models import sampleaap
from django_extensions.admin import ForeignKeyAutocompleteAdmin
class sampleadmin(ForeignKeyAutocompleteAdmin):
related_search_fields = {
'student': ('stud_name', 'city'),
}
fields = ('student', ' ', 'is_active')
admin.site.register(sampleapp, sampleadmin)
Explanation:
In the above, the student is our foreign key attribute used to search stud_name and city.
Django Extensions Project
Let’s see what the extension of the project is as follows:
Django provides different extensions as follows:
1. Debugger Tags
Using Debugger tags, we can easily debug templates with three types of filters as follows.
- IPDB: It is used to debug the model object.
- PDB: It is a normal debugger to debug a model object.
- WDB: It is a Web Debugger tool.
2. Field Extension
- RandomCharField: If we want to create random characters with a specified length, we can use this extension.
- JSONFiled: It is used to serialize the JSON objects.
3. Model Extension
- ActivatorModel: It is used to check the status of the class. It also provides different extensions to the user, such as validators, command extensions, etc.
Django Extensions Command
Given below are the Django extensions command:
- create_app: By using this command, we can create an application with a specified name. By using this command, we can determine the layout choice where you can show a format catalog construction to use as your default.
- create_command: Create a command expansion registry structure inside the predetermined application. This makes it simple to get everything rolling by adding an order expansion to your application.
- create_jobs: We can create the directory structure as per our specified application.
- create_superuser: We can easily make the super user for Django.
- describe_form: If we need to display any information for the specified model.
- dumpscript: Creates a Python script that will repopulate the information base utilizing objects. The upside of this approach is that it is straightforward and more adaptable than populating the information base or utilizing XML.
- export_emails: As per our requirement, we can export the email address in a different format, and it supports Google, outlook, LinkedIn, etc.
- generate_secret_key: We can easily create a new secret key.
- graph_model: Sometimes, we need to send the output file; at that time, we can use this command to create a graph of our specified model.
- password: As per our requirement, we can reset the user password.
- print_user_for_session: If we want to print the information or any other data for the specified session key, then we can use this command.
- shell_plus: If we enhance the Django shell version, it will auto-load all our models.
- show_urls: If we’re going to see the route of URLs in our project, then we can use this command.
- sqldiff: By using this command, we can perform sanity testing on a specified database whenever required.
Installing Django Extension
Let’s see how we can install an extension in Django as follows:
First, we must ensure the installation of Python; if we have Python, then we can directly use the below command to install Django extensions; if we don’t have Python, then we need to install Python first and then use the below command.
Code:
$pip install django-extensions
Explanation:
- The Django extension is hosted by GitHub; normally, the development version of the Django extension should be stable.
- After installing the extension, if we want to check the version of Django, we need to use the below command as follows.
Output:
Implementing Django Extension
Let’s see how we can implement the Django extension as follows:
In the above point, we have already discussed how to install the Django extension; after successfully installing it, we need to configure it. So first, we need to create a different environment with the help of the below commands.
Code:
mkdir sample_auth
cd sample_auth
python –m venv env
source env/bin/activate
pip install django
We need to create an application inside the project, open the setting .py file, and add the created application to the list.
Code:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'samplapp',
'django_extensions'
]
Example of Django Extensions
Let’s consider we have a student model, so add the following code to the model.py.
Code:
from django.db import models
class StudentModel(models.Model):
id = models.IntegerField(primary_key=True)
stud_name = models.CharField(max_length=20)
name = models.TextField()
def __str__(self):
return f"Student: {self.stud_name}"
Now inside the admin.py file, we need to apply for the admin extension.
Code:
from .models import *
from django_extensions.admin import ForeignKeyAutocompleteAdmin
class StudentAdmin(ForeignKeyAutocompleteAdmin):
fields = {
'Student':('stud_name',)
}
fields =('stud_name','remark_text','studnet')
Now run the server and check the admin model as shown below screenshot.
Output:
Conclusion
With the help of the above article, we saw about the Django extensions. From this article, we saw basic things about the Django extensions and the features and installation of the Django extensions, and how we use them in the Django extensions.
Recommended Articles
We hope that this EDUCBA information on “Django Extensions” was beneficial to you. You can view EDUCBA’s recommended articles for more information.