[Django]-Django Application: Foreign Key pointing to an abstract class

7👍

An abstract class is a class that doesn’t exist. It is used as a basis for other classes. It is never never ever initialized.

Something that does not exist cannot have a foreign key pointing at it!

Something to look at, if you want to have a way to point at several different kinds of classes: generic relations. This is Django’s build-in way to have something that looks like a foreign key point at a number of different objects.

2👍

It is imposible.
Think what would happen to all the classes with a foreign key pointing A if A is abtract and several classes inherit from A.

I dont know your requirements but I maybe you should consider using multitable inheritance, and point the FK to the parent table.

From the Django documentation:

Multi-table inheritance

https://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance

The second type of model inheritance supported by Django is when each
model in the hierarchy is a model all by itself. Each model
corresponds to its own database table and can be queried and created
individually. The inheritance relationship introduces links between
the child model and each of its parents (via an automatically-created
OneToOneField). For example:

👤fdisk

Leave a comment