Django models inheritance

I want to create base model for my submodels, for example: Car(Base) and Truck, Sportcart etc. And I want to bind them all using base class with another class, cuz I don't want to bind all of them separately with another class. Example: I have a user and I want to add to him cars (I will do it separately for each class, yes). And I want to get all of them using my base Car class.

Upvotes: 1

Views: 148

Answers (1)

Alain Bianchini
Alain Bianchini

Reputation: 4171

Mark your base model as abstract:

class Car(models.Model):
    # Your fields
    class Meta:
        abstract = True

Then inherit from the abstract model:

class SportCar(Car):
    # Your fields
    pass

In this case Django will make migrations only for SportCar, because Car is an abstract model.

Upvotes: 1

Related Questions