Free eBook - Django 3 Web Development Cookbook - Fourth Edition

3.5 (2 reviews total)
By Aidas Bendoraitis , Jake Kronika
  • A new free eBook every day on the latest in tech
  • 30 permanently free eBooks from our core tech library
  1. Getting Started with Django 3.0
About this book
Django is a web framework for perfectionists with deadlines, designed to help you build manageable medium and large web projects in a short time span. This fourth edition of the Django Web Development Cookbook is updated with Django 3's latest features to guide you effectively through the development process. This Django book starts by helping you create a virtual environment and project structure for building Python web apps. You'll learn how to build models, views, forms, and templates for your web apps and then integrate JavaScript in your Django apps to add more features. As you advance, you'll create responsive multilingual websites, ready to be shared on social networks. The book will take you through uploading and processing images, rendering data in HTML5, PDF, and Excel, using and creating APIs, and navigating different data types in Django. You'll become well-versed in security best practices and caching techniques to enhance your website's security and speed. This edition not only helps you work with the PostgreSQL database but also the MySQL database. You'll also discover advanced recipes for using Django with Docker and Ansible in development, staging, and production environments. By the end of this book, you will have become proficient in using Django's powerful features and will be equipped to create robust websites.
Publication date:
March 2020


Models and Database Structure

In this chapter, we will cover the following topics:

  • Using model mixins
  • Creating a model mixin with URL-related methods
  • Creating a model mixin to handle creation and modification dates
  • Creating a model mixin to take care of meta tags
  • Creating a model mixin to handle generic relations
  • Handling multilingual fields
  • Working with model translation tables
  • Avoiding circular dependencies
  • Adding database constraints
  • Using migrations
  • Changing a foreign key to the many-to-many field


When you start a new app, the first thing that you do is create the models that represent your database structure. We are assuming that you have already created Django apps, or, at the very least, have read and understood the official Django tutorial. In this chapter, you will see a few interesting techniques that will make your database structure consistent across the different apps in your project. Then, you will see how to handle the internationalization of the data in your database. After that, you will learn how to avoid circular dependencies in your models and how to set database constraints. At the end of the chapter, you will see how to use migrations to change your database structure during the process of development.


Technical requirements


Using model mixins

In object-oriented languages, such as Python, a mixin class can be viewed as an interface with implemented features. When a model extends a mixin, it implements the interface and includes all of its fields, attributes, properties, and methods. The mixins in Django models can be used when you want to reuse the generic functionalities in different models multiple times. The model mixins in Django are abstract base model classes. We will explore them in the next few recipes.

Getting ready

First, you will need to create reusable mixins. A good place to keep your model mixins is in a myproject.apps.core app. If you create a reusable app that you will share with others, keep the model mixins in the reusable app...


Creating a model mixin with URL-related methods

For every model that has its own distinct detail page, it is good practice to define the get_absolute_url() method. This method can be used in templates and also in the Django admin site to preview the saved object. However, get_absolute_url() is ambiguous, as it returns the URL path instead of the full URL.

In this recipe, we will look at how to create a model mixin that provides simplified support for model-specific URLs. This mixin will enable you to do the following:

  • Allow you to define either the URL path or the full URL in your model
  • Generate the other URL automatically, based on the one that you defined
  • Define the get_absolute_url() method behind the scenes

Getting ready


Creating a model mixin to handle creation and modification dates

It is common to include timestamps in your models for the creation and modification of your model instances. In this recipe, you will learn how to create a simple model mixin that saves the creation and modification dates and times for your model. Using such a mixin will ensure that all of the models use the same field names for the timestamps, and have the same behaviors.

Getting ready

If you haven't yet done so, create the myproject.apps.core package to save your mixins. Then, create the file in the core package.

How to do it...


Creating a model mixin to take care of meta tags

When you optimize your site for search engines, you not only have to use semantic markup for each page, but you also have to include appropriate meta tags. For maximum flexibility, it helps to have a way to define content for common meta tags, specific to objects that have their own detail pages on your website. In this recipe, we will look at how to create a model mixin for the fields and methods related to the keyword, description, author, and copyright meta tags.

Getting ready

As detailed in the previous recipes, make sure that you have the myproject.apps.core package for your mixins. Also, create a directory structure, templates/utils/includes/, under the package, and inside...


Creating a model mixin to handle generic relations

Aside from normal database relationships, such as a foreign-key relationship or a many-to-many relationship, Django has a mechanism to relate a model to an instance of any other model. This concept is called generic relations. For each generic relation, we save the content type of the related model as well as the ID of the instance of that model.

In this recipe, we will look at how to abstract the creation of generic relations in the model mixins.

Getting ready

For this recipe to work, you will need to have the contenttypes app installed. It should be in the INSTALLED_APPS list in the settings, by default, as shown in the following code:

# myproject/settings/


Handling multilingual fields

Django uses the internationalization mechanism to translate verbose strings in the code and templates. But it's up to the developer to decide how to implement the multilingual content in the models. We'll show you a couple of ways for how to implement multilingual models directly in your project. The first approach will be using language-specific fields in your model.

This approach has the following features:

  • It is straightforward to define multilingual fields in the model.
  • It is simple to use the multilingual fields in database queries.
  • You can use contributed administration to edit models with the multilingual fields, without additional modifications.
  • If you need to, you can effortlessly show all of the translations of an object in the same template.
  • After changing the amount of languages in the settings, you will need to create and run...

Working with model translation tables

The second approach to handling multilingual content in the database involves using model translation tables for each multilingual model.

The features of this approach are as follows:

  • You can use contributed administration to edit translations as inlines.
  • After changing the amount of languages in the settings, no migrations or other further actions are necessary.
  • You can effortlessly show the translation of the current language in the template, but it would be more difficult to show several translations in specific languages on the same page.
  • You have to know and use a specific pattern described in this recipe for creating model translations.
  • It's not that simple to use this approach for database queries, but, as you will see, it's still possible.

Avoiding circular dependencies

When developing Django models, it is very important to avoid circular dependencies especially in the files. Circular dependencies are imports in different Python modules from each other. You should never cross-import from the different files, because that causes serious stability issues. Instead, if you have interdependencies, you should use the actions described in this recipe.

Getting ready

Let's work with categories and ideas apps to illustrate how to deal with cross dependencies.

How to do it...

Follow these practices...


Adding database constraints

For better database integrity, it's common to define database constraints, telling some fields to be bound to fields of other database tables, making some fields unique or not null. For advanced database constraints, such as making the fields unique with a condition or setting specific conditions for the values of some fields, Django has special classes: UniqueConstraint and CheckConstraint. In this recipe, you will see a practical example of how to use them.

Getting ready

Let's start with the ideas app and the Idea model that will have at least title and author fields.

How to do it...


Using migrations

In Agile software development, requirements for the project evolve and get updated from time to time in the process of development. As development happens iteratively, you will have to perform database schema changes along the way. With Django migrations, you don't have to change the database tables and fields manually, as most of it is done automatically, using the command-line interface.

Getting ready

Activate your virtual environment in the command-line tool, and change the active directory to your project's directory.

How to do it...

To create...


Changing a foreign key to the many-to-many field

This recipe is a practical example of how to change a many-to-one relation to a many-to-many relation, while preserving the already existing data. We will use both schema and data migrations in this situation.

Getting ready

Let's suppose that you have the Idea model, with a foreign key pointing to the Category model.

  1. Let's define the Category model in the categories app, as follows:
# myproject/apps/categories/
django.db import models
from django.utils.translation import gettext_lazy as _

from myproject.apps.core.model_fields import MultilingualCharField

class Category(models.Model):
title = MultilingualCharField(
About the Authors
  • Aidas Bendoraitis

    Aidas Bendoraitis has been professionally building websites for the past 18 years. For the last 14 years, he has been working at a design company, studio 38 pure communication, in Berlin. Together with a small dedicated team, he has mostly used Django in the backend and jQuery in the frontend to create cultural and touristic web platforms.Among different side projects, he is bootstrapping a SaaS business with strategic prioritizer 1st things 1st. Aidas Bendoraitis is active on Twitter and other social media under the username DjangoTricks.

    Browse publications by this author
  • Jake Kronika

    Jake Kronika, a senior software engineer with nearly 25 years' experience, has been working with Python since 2005, and Django since 2007. Evolving alongside the web development space, his skillset encompasses HTML5, CSS3, and ECMAScript 6 on the frontend, plus Python, Django, Ruby on Rails, Node.js, and much more besides on the server side. Currently a senior software engineer and development team lead, he collaborates with skilled designers, business stakeholders, and developers around the world to architect robust web applications. In his spare time, he also provides full-spectrum web services as sole proprietor of Gridline Design and Development. Prior to this book, he has acted as a technical reviewer for several other Packt titles.

    Browse publications by this author
Latest Reviews (2 reviews total)
So far so good. Lots of knowledge to learn.
Still waiting for it to publish
Recommended For You
Web Development with Django

Learn how to create your own websites simply, safely, and quickly with Django by tackling practical activities based on realistic case studies

By Ben Shaw and 4 more
The Python Workshop

Learn the fundamentals of clean, effective Python coding and build the practical skills to tackle your own software development or data science projects

By Andrew Bird and 4 more
Machine Learning for Algorithmic Trading - Second Edition

Leverage machine learning to design and back-test automated trading strategies for real-world markets using pandas, TA-Lib, scikit-learn, LightGBM, SpaCy, Gensim, TensorFlow 2, Zipline, backtrader, Alphalens, and pyfolio.

By Stefan Jansen
Data Engineering with Python

Build, monitor, and manage real-time data pipelines to create data engineering infrastructure efficiently using open-source Apache projects

By Paul Crickard