Introduction:
In
this article I will explain you how to add autocomplete for Foreignkeys in Django admin panel. Sometimes you might come across
the case where you need to map existing records or create records
from django admin panel which has foreignkey relationships, now if
there are some hundreds or thousands of records present in Foreignkey
model than it becomes difficult to map it or select the required
foreignkey because django does not provides search function while
creating record, so to overcome the problem we have added autosearch
filter in Foreignkeys.
Lets
start with Basic Example:
Lets
start with basic django project, where we have to create record for
different students and map them to different courses. Now for
creating the django project we need to a create virtual environment
and install django in it.
We
can create virtual environment using `virtualenv env`, which will
create `env` for us, now we can install django after activating the
environment using `source env/bin/activate` and `pip install
Django==1.11.3`, right now I am installing django 1.11.3 version.
Now,
create the project `adminFilter` using `django-admin startproject
adminFilter`. Once you are done with it, create an app called support
where we will create `Course` and `Student` models using `python
manage.py startapp support`, also update the `INSTALLED_APPS` with
`support` app in settings.py
`adminFilter/settings.py`
INSTALLED_APPS
= [
..,
‘support’,
]
Design
Models and Register in admin panel:
Now
add the following two models in `models.py` of your support app.
`support/models.py`
class
Course(models.Model):
courseName
= models.CharField(max_length=254)
fees
= models.DecimalField(max_digits=20, decimal_places=2)
def
__str__(self):
return
self.courseName
class
Student(models.Model):
firstName
= models.CharField(max_length=254)
lastName
= models.CharField(max_length=254)
rollNumber
= models.CharField(max_length=254, unique=True)
course
= models.ForeignKey(Course)
Now,
once the models are designed create migrations and migrate them in db
using following commands
`python
manage.py makemigrations`
`python
manage.py migrate`
-
Also create a superuser, so that you can login as admin and see the
model details in panel using
`python
manage.py createsuperuser`.
-
Now, register the designed models in admin panel, for that you need
to import them to admin.py file of support and and register the
models.
`support/admin.py`
from
.models import Course, Student
admin.site.register(Course)
admin.site.register(Student)
-
For now I am creating some courses, so that we can see the details in
admin panel, now creating student from admin panel, here we donw have
any option to search the course the only way is to scroll the options
but if there are hundreds or thousands of options are present that it
becomes difficult to map the course.
Mapping
Course with Student.
Configuring
Autocomplete For ForeignKey in Admin Panel:
Now,
to configure Autocomplete we need to install
`django-autocomplete-light` and update `INSTALLED_APPS` of
settings.py file and admin.py file.
Install
`django-autocomplete-light` by following command `pip install
django-autocomplete-light` and than update settings.py and admin.py
file with following code.
`adminFilter/settings.py`
INSTALLED_APPS
= [
'dal',
'dal_select2',
.........
]
`support/admin.py`
from
django.contrib import admin
from
django import forms
from
dal import autocomplete
from
.models import Course, Student
class
StudentAdminForm(forms.ModelForm):
class
Meta:
model
= Student
fields
= ('__all__')
widgets
= {
'course':
autocomplete.ModelSelect2(url='course-autocomplete')
}
class
StudentAdmin(admin.ModelAdmin):
form
= StudentAdminForm
list_display
= ('firstName', 'course', 'lastName', 'rollNumber')
search_fields
= ['course__courseName',]
admin.site.register(Course)
admin.site.register(Student,
StudentAdmin)
-
Now, restart the server and create one record for student and map the
course, you will see a autocomplete bar in Foreignkey from where you
can easily map it to required course out of hundreds of courses.
Autocomplete
for Course Foreign key
Autocomplete for Course Foreign key
Conclusion:
We
can easily add autocomplete for any foreignkey param in django admin
using django autocomplete. Code can be obtain from github repo for
reference `https://github.com/pawan3103/djangoAdminAutocomplete`
0 Comments