Search code examples
pythonpython-3.xdjangodjango-modelsabstract-class

Are django abstract models really that useful?


I have recently come across this in one of django related blog post!

class Client(models.Model):
   name = models.CharField(max_length=255, required=True)
   # .. other fields
   class Meta:
     abstract = True

After reading the docs I could not understand what the purpose of "abstract = True" is. Any help will be useful. Thanks!


Solution

  • From the docs

    Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put abstract=True in the Meta class. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class.

    Personally I have found that one of many advantages of using abstract models is they allow in software extension and prohibit modifications as per SOLID principles.