Let’s look at the most common mistakes even experienced Django developers make, which even the most successful projects can encounter sooner or later.
Content:
How Django Works
Like any other framework, Django is a software platform (framework) on which the developer “hinges” additional application components, such as:
- User interface.
- Scripts (scenarios) of application/site elements.
- Software libraries.
- Multimedia content (photos, video, audio).
- Security modules.
- Databases, etc.
The framework is responsible for the basic aspects of the product. And plug-ins are responsible for the specific functions of a particular application.
Using frameworks allows:
- Speed up development.
- Simplify support.
- Avoid application errors.
Reinventing the Wheel
Django has a lot of out-of-the-box tools and a huge number of out-of-the-box third-party libraries. If you have a problem, try searching for it before you start solving it, because chances are the solution has already been found before you.
You can also use the Django Projects online catalog, where under the category “apps” there are already thousands of ready-made applications.
Monolithic Application Structure
Although Django can be developed in different ways, experienced developers recommend sticking to standard methods.
The basis of the framework is a Django project which comprises one or more applications. Each application is a standalone package, which is usually required for one thing. For example, these couldn’t be online slots but could be a blog, event calendar, authorization, shopping cart, etc.
Too Many Presentation Queries or Unoptimized Queries
Django’s object-oriented mapping mechanism (ORM) is often accused of making too many queries or making unoptimized queries. But the same thing can happen with ORM in other frameworks.
The real problem is that we are often unaware of performance problems and their sources.
One great debugging tool is Django-debug-toolbar. This library will help you to quickly find the source of the problem. You can use the library to trace problems in SQL queries, templates, cache, etc.
After identifying the causes of performance problems, you can choose the right way to fix them. Among other solutions, the following steps can be taken:
- Configure simple ORM queries (pay attention to prefetching).
- Configure and optimize ORM queries.
- Add caching where needed.
- Provide more resources.
Redundant Model Fields
Because queries cannot use computable columns, developers often duplicate fields that represent the same data in different ways.
For example, half of the Vehicles model records will have s_motorcycle == True and wheel_count == 4, and you can’t be sure which field to trust (hint: none). With Django, you can refactor such properties using the @property decorator. However, even if the ORM allows you to refer to columns as properties, the reverse is not true, meaning you need to refactor each query manually.
Lack of Indexes in Models
Even experienced Django developers sometimes forget about indexes, so it’s important to always add indexes to your models. On the other hand, you have to not overdo it here, as it can slow down insertions, updates, and deletions.
An index is needed where you will use filtering and joins. Revisit your QuerySets to determine where you need to use the index.
Summary
Django was designed to create complex web projects quickly and easily. However, if you rush to add new features, you can easily miss something or lose the overall picture of the project.