In my opinion, this article describes a really easy way how to painfully shoot yourself in the foot when you store your data in SQL databases.
In my experience, introducing abstract models in Django leads to unsolvable N+1 query issues in the long run.
Imagine following scenario:
You have an abstract model Person with field full_name.
You have a class Student inheriting from Person.
You have a class Teacher inheriting from Person.
Imagine you have a model called AccessCard.
This AccessCard has properties value (int) and owner (Person). When you create a table listing 100 AccessCards per page, showing both the AccessCard.value and AccessCard.owner.full_name, is there a way, how to provide these data this using just 1 SQL query instead of 1+100?
If I remember correctly, it is easy when abstract classes are not used. See QuerySet.prefetch_related / QuerySet.select_related. However when I was trying to find a solution for this with inheritance in play, I failed miserably.
In my experience with ORM mappers in Django and with Doctrine from the PHP world, using inheritance in conjunction with SQL is a bad idea.
The time you save by not typing again the definitions are definitely lost when you try to optimize the horribly inefficient queries.
I do not want to degrade the value of your article, I just want to point out the downsides and that this approach has, in my opinion, significant drawbacks which should be pointed out.
In my opinion, this article describes a really easy way how to painfully shoot yourself in the foot when you store your data in SQL databases.
In my experience, introducing abstract models in Django leads to unsolvable N+1 query issues in the long run.
Imagine following scenario:
Imagine you have a model called AccessCard.
This AccessCard has properties value (int) and owner (Person). When you create a table listing 100 AccessCards per page, showing both the AccessCard.value and AccessCard.owner.full_name, is there a way, how to provide these data this using just 1 SQL query instead of 1+100?
If I remember correctly, it is easy when abstract classes are not used. See QuerySet.prefetch_related / QuerySet.select_related. However when I was trying to find a solution for this with inheritance in play, I failed miserably.
In my experience with ORM mappers in Django and with Doctrine from the PHP world, using inheritance in conjunction with SQL is a bad idea.
The time you save by not typing again the definitions are definitely lost when you try to optimize the horribly inefficient queries.
I do not want to degrade the value of your article, I just want to point out the downsides and that this approach has, in my opinion, significant drawbacks which should be pointed out.
You can use
QuerySet.prefetch_related
orQuerySet.select_related
with Abstract Base Classes in Django.Let's say we have these models:
If you make this query you wouldn't have a N+1 issue:
You have one query only. The generated SQL is:
Maybe you were referring to something else?