Domain-Driven Design with Django
by Federico Marani
Django is a collection of components that are designed to work together, and they cover many of the common areas of web development. When you browse online, you encounter many different sites that look different on the surface, but in reality contain the same patterns. Every website serves HTML, the language that represents the content of the website, and CSS, the language that declares how the content is rendered. Most websites have the possibility to log in to a private area, where you can see customizations created for the user and data that may not be public. Django supports all of this with its components. An important aspect of Django is that many of its components are independent, and therefore can be used without having to load all of them. That said, some of the more high-level components leverage existing ones.
Django is a framework that follows the Model-Template-View pattern, which is very similar to Model-View-Controller pattern that many other frameworks have. The model layer represents the database layer, used for data storage. Django abstracts you from writing SQL queries. Instead of SQL, you use Python objects, and the load/save operations are handled for you. The Template layer is responsible of composing the HTML responses. It allows you to divide the HTML in sections, incorporate these sections in multiple pages, do on-the-fly transformations and generation of data, and many other operations. The View layer is the layer that sits in between database and HTML, and is normally the biggest layer in an application. It is where most business logic is located. It is responsible for telling the template layer what output to generate, to pass the data that needs to be returned to the user, to handle form submissions, and to interface with the model layer for persisting data.
Domain-driven design (DDD for short) is a technique to write software. It focuses on the domain, which is the knowledge or the activity that the software needs to model and support. During the development, concepts from the domain will be incorporated with the help of business experts and engineers. What Django can bring to this is focus. Due to the fact that engineers can leverage existing code for many of the functionalities that are not domain-specific, delivering features is usually faster. Some of the concepts of DDD are present in Django. Entities, Repositories, Aggregates, and Value Objects resemble the abstractions of ORM models, managers, or HTTP request objects. The code that is missing from Django might be the code specific to your business, and that is where DDD can help.
About the Author
Federico Marani has been a freelance software engineer and DevOps specialist for over 15 years. With a background in Computer Science and a Msc in Business Psychology, he has worked in companies ranging from e-commerce, finance, medical and banking companies. He started developing web applications with Python 1.5.2 on RedHat systems and has been using Django since the first 1.x versions, more than 8 years ago. He now consults on technology strategy and tech teams with startups, and is still very hands-on with coding. He has open sourced a lot of code online, some of which is part of GNU Social. In his spare time, he enjoys playing drums and running.
This article was contributed by Federico Marani, author of Practical Django 2 and Channels 2.