Simple Trick To Improve Django Admin Search Results

September 19, 2017 • Peter Sanchez

The Django admin application is pretty amazing. It’s a quick and easy way to implement a CRUD interface to your projects data. Getting the admin area up and running is a breeze but that doesn’t mean that the admin interface isn’t powerful or versatile. Nearly every aspect of the admin application has a way to be customized to fit the unique needs of your project.

Over the years we’ve customized all sorts of pieces of the admin for one client or another. However, one thing that we see over and over is how to easily surface specific objects (content, photos, whatever) in the admin area from search.

Story time… Our client Acme Blog Inc. has a problem. They’re wildly successful and have built up a ton of popular blog posts over the years. As a popular content site their editors frequently need to locate specific pieces of content in the admin area for updates. This routinely turns into a needle in a haystack type of situation.

Say an editor needs to make an update to the article titled “How To Use Pinterest To Find Cool DIY Ideas”. The editor goes to the Django admin area where their BlogPost model lives, and searched for “how to diy”. They’re hit with hundreds of results. Why? Because by default, the admin will search through all search fields that contain the words “how”, “to”, and “diy”. You can imagine how this can match practically every single post in the company’s inventory. You can also imagine how time consuming this can be for editors to locate the exact content they’re looking for.

Das Code

Let’s look at some hypothetical code for the models and model admins for Acme Blog’s project. Let’s start with the model class…

Simple enough. Now let’s look at the model admin…

So this is a very basic, and pretty standard, example of a blog post model and it’s model admin. So what we’re going to do is improve the search results so that an editor can search for the posts exact title or slug and those results filter to the top, making it easy to find the content they need, while keeping the rest of the results in tact.

To do this, we need to write a Mixin. Here’s what it would look like…

Here’s what this Mixin is doing is:

  • Overriding the get_search_results method

  • Performing the normal Django admin search functionality

  • Annotating the default results with an integer value for “exact_title” and “exact_slug”, defaults to 0 but if there is a match, sets it to 1

  • Checks if an exact title or slug match exists, if so, adds appropriate ordering depending on the condition matched

  • Orders the results as needed to ensure that the relevant posts rise to the top of the search results

Essentially we just add some conditional expressions to check for the matches and then filter when there is a match.

Now to add the mixin to the model admin we just make the following change to the BlogPostAdmin class…

Done and done.

While this situation is fairly simple you can do pretty complex operations here to tune your search results. For instance, using django-haystack or a Redis index to search your content.

There you have it. The Django admin is a powerful application and a huge selling point for the Django framework in general. The larger projects become, the more you want to customize to fit the specific project needs. Luckily, odds are, the Django project is probably a step ahead and given you the outlet to do just that.

Further reading: Haki Benita wrote a great post on improving admin performance for larger projects titled Things You Must Know About Django Admin As Your App Gets Bigger.

blog comments powered by Disqus