How to Create Autocomplete Fields for User Selection in Django Admin

Vignesh N U
2 min readJul 11, 2023

--

The Django admin panel provides powerful features for managing data effectively. One such feature is the ability to use autocomplete fields when selecting User models as foreign keys. In this tutorial, we will explore how to implement autocomplete fields for both the User and Blog models in the Django admin panel. This will enable easy searching and selection of users while creating blog posts, enhancing the user experience and simplifying data management.

Step 1: Setting up the Models: Let’s begin by creating the User and Blog models that will be used in our example. Place the following code in your models.py file:

from django.contrib.auth.models import User
from django.db import models

class Blog(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)

Step 2: Implementing Autocomplete Fields in Django Admin: To create autocomplete fields for both the User and Blog models in the Django admin panel, follow these steps:

2.1 In your admin.py file, import the necessary modules and classes:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from .models import Blog

2.2 Customize the UserAdmin class by adding the following code in your admin.py file:

UserAdmin.search_fields = ('username', 'email')

2.3 Define the BlogAdmin class and configure autocomplete:

class BlogAdmin(admin.ModelAdmin):
autocomplete_fields = ['author']

2.4 Register the models with the admin classes:

admin.site.unregister(User)
admin.site.register(User, UserAdmin)
admin.site.register(Blog, BlogAdmin)

Step 3: Understanding the Implementation: Let’s examine the code and understand how the autocomplete fields and searching functionality work for both the User and Blog models in the Django admin panel.

  • In Step 2.2, we customize the UserAdmin class by specifying search_fields. This enables searching for User models based on their username or email. When we unregister and re-register the User model with the modified UserAdmin class, these customizations take effect.
  • In Step 2.3, we define the BlogAdmin class and set autocomplete_fields to ['author']. This configures an autocomplete field for the author foreign key in the Blog model. The autocomplete feature enhances user selection by allowing them to search and select users based on their username or email.
  • Finally, in Step 2.4, we unregister and re-register the User and Blog models with their respective admin classes, ensuring that the customizations are applied to both models in the admin panel.

By implementing autocomplete fields and enabling searching for both the User and Blog models in the Django admin panel, we have simplified the process of selecting users while creating blog posts. Users can now search and select the desired author using their username or email, improving the user experience and streamlining data management. By following the steps outlined in this tutorial and placing the code in the appropriate files, you can easily integrate autocomplete fields and searching functionality into your Django project.

--

--

Vignesh N U

Full-stack developer passionate about user-friendly web and mobile apps. Always exploring new tech. Follow me on Medium for insights on coding, career & travel.