How to use SOLID principles in Django

I’m a student from Universidade Federal do Ceará (Brazil), and my course completion work is about refactor and optimization a system that was made with Django.

As for refactoring, I will try to use the principles that Martin Fowler left in his book - basic things about programming and good practices, regardless of the programming language.

When analysing and refactoring the system design, I thought about taking advantage of SOLID principles to improve the organization of entities and models. However, researching these principles in Django, I saw that concepts such as interfaces, inversion and dependency injection are not implemented and provided directly by Django and that it would be a risk to work with libraries that do this, as the internal behaviour of the models and entities could be affected and harmed (Django with Abstract Base Classes & Composition).

My question is, does Django somehow use concepts similar to interfaces, inversion and dependency injection? Even if this is done at lower layers of the framework. Just as an explanation for the work: how each of the SOLID concepts can be used in this system, or in this case, how they cannot be used, if the concept is not present in Django.

Hello there, and greetings from Brazil as well!

It really depends on where are you trying to apply these concepts, Django for example gives you the ORM that is an abstraction that is most likely “injecting” a different database backend according to your settings, and you should’nt worry about the specifics of the database that you picked, and this a great example of SOLID.

Since Django is a framework, most of it is configurable, and it has to cover many scenarios that your application may not even encounter.

I tend to not overlook SOLID, because in real-life when things change, they change a lot, and most of abstractions that we make in a recent-past are no longer enough for the new requirements. So I tend to abstract, and inject-dependencies at runtime on external integrations, such as payment. So my code depends on interfaces for those particular external components.