Dino Geek, try to help you

Why does Django create a table for abstract models?


Django, the high-level Python Web framework that encourages rapid development and clean, pragmatic design, does NOT actually create a database table for abstract base models.

According to the official Django documentation, an abstract base class, in the context of Django, is a model class that is not intended to be used as a standalone model. Instead, other models inherit from the abstract base class, gaining the fields and methods of the abstract model.

So the misunderstanding lies in the term “abstract.” In Django, an abstract base model holds information that you want to include in other models, consistently, across your application. They are a tool for reducing redundancy in your code by defining common information centrally within an abstract model, which can then be integrated into multiple child models.

Django achieves this through inheritance. When additional models inherit from the abstract base model, Django creates a table for each of these additional models with fields derived from those in its abstract base model parent, along with any extra fields defined in the inheriting models themselves.

However, no table is created for the abstract model itself. To make Django treat a model as abstract, you include `abstract = True` in its Meta class, as follows:

```
class CommonInfo(models.Model): name = models.CharField(max_length=100) age = models.PositiveIntegerField()

class Meta: abstract = True ```

In this example, if you would add an inheritance to another model like below:

```
class Student(CommonInfo): home_group = models.CharField(max_length=5)
```

Django would create a database table for the `Student` model with fields: `name`, `age`, and `home_group`. However, it would not create a table for `CommonInfo`.

In conclusion, Django doesn’t create a database table for an abstract base model, rather it creates tables for each model that inherits from the abstract base model. The table for each child model includes fields inherited from the abstract model, in addition to any fields defined directly on the child model.

Sources:
- Django Documentation – Model Inheritance:
```
https://docs.djangoproject.com/en/3.2/topics/db/models/#model-inheritance
```

- Django Documentation – Abstract base classes:
```
https://docs.djangoproject.com/en/3.2/topics/db/models/#abstract-base-classes
```


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use