5 Useful Django Tips

Gustav Willig
3 min readJul 31, 2022

It’s been a long time since the first release of the Django.

Before we will dive deeper into django’ itself, let’s do a small throwback into django’s history.

History

Django was developed in 2003 by Simon Willison and Adrian Holovaty. Both were working for the Lawrence Journal-World newspaper. The web development team had to implement the new features or entire applications within just a few hours, and the two started using Python.

1. Specialising the admin view

The automatic admin interface is one of Django’s most potent features. In order to create a rapid, model-centric interface where vetted people can manage content on your website, it reads metadata from your models. You can examine all of your application’s models and instances of those models, i.e. all of your users and their posts, using the basic admin view settings. Specialising this can give your apps admin a better view of the applications data. I like to order them by creation date and to add a search field. An example can be seen below

from django.contrib import admin
from .models import Entry
from simple_history.admin import SimpleHistoryAdmin


@admin.register(Entry)
class BlogPost(SimpleHistoryAdmin):
ordering = ('-creation_date',)
list_display = ['creation_date', 'title', 'created_at']
search_fields = ['text']
date_hierarchy = 'creation_date'

You can find more info about Django admin site here.

2. Move to class based views

Class Based Views (CBVs) used to make me feel quite compatible, but as I moved away from method based views (MBVs) I started to enjoy them. The simplicity of CBVs gives them a wonderful appearance. The amount of code involved is so minimal that it doesn’t seem to make sense at first. You can find more information here.

3. Writing Fat Views and Skinny Models

Instead of writing your application logic in models, you should be writing it in views, which makes the views “fat” and your models “skinny.”
Write “fat models, skinny views” instead.
On your models, break logic down into manageable ways. Instead of copying and pasting a ton of code, you may utilize it several times from different sources (admin interface UI, front-end UI, API endpoints, multiple views) in just a few lines of code. Therefore, the next time you need to email a user, add an email method to the model instead of writing this logic in your controller.

4. Store variables in a .env file

There are times in Django where you have to provide sensitive information in the backend. This is as simple as the Django Secret Key or to more serious information like the password and user name for user database. Store this information .env file instead of the settings.py file. I like to use the lib dotenv

import os
from dotenv import load_dotenv
project_folder = os.path.expanduser('~/my-project-dir')
load_dotenv(os.path.join(project_folder, '.env'))
SECRET_KEY = os.getenv("SECRET_KEY")

5. Use .get() instead of Models.objects.all()[0]

In a Django views, you should ty to avoid using Models.objModels.objects.all()[0] rather you should use Models.objects.get(condition). The get() methode retrieves only one record. If no record exists that meets the given criteria, it raises a DoesNotExist exception. If more than one record with the given criteria exists, it raises a MultipleObjectsReturned exception. So the reader of your code will know immediately that the record should be unique for the fields you filtered on.

Another great Django teach is Simple is better than complex. He has tutorials that will take you through Django from start to deploying.

--

--

Gustav Willig

An AI Full-Stack Developer with a passion for using data to drive business decisions. Get your latest news about Django and AI trends by subscribing