How to Inherit objects in django?


is there a link or tutorial on how to inherit an objects in django? let us say that i have a vehicle as a parent and a car and a truck for it's child.

if it is possible, is it done in the models.py? and how is it work?



Exactly the same as python inheritence

class Vehicle(Model): name = models.TextField() class Car(Vehicle): passengers = PositiveIntegerField() class Truck(Vehicle): tonnage = FloatField() >>> Car.objects.create(name='Beetle', passengers = 5) <Car: name="Beetle",passengers=5> >>> Truck.objects.create(name='Mack', tonnage=4.5) <Truck: name="Mack,tonnage=4.5> >>> Vehicle.objects.all() [<Vehicle: name="Beetle">,<Vehicle: name="Mack>] >>> v = Vehicle.objects.get(name='Beetle') >>> (bool(v.car), bool(v.truck)) (True, False) >>> v.car <Car: name="Beetle",passengers=5> >>> v.truck None

<a href="https://docs.djangoproject.com/en/dev/topics/db/models/#model-inheritance" rel="nofollow">https://docs.djangoproject.com/en/dev/topics/db/models/#model-inheritance</a>


It's worth noting that while Django supports a couple of inheriting methods, none of them behave in a polymorphic manner, that is, your can't make a query on a Vehicle model and get a Car instance if you use an abstract base class, and if you use Multi table inheritance, you can't use the behavior of the subclass from a base class model instance.

There are <a href="https://github.com/bconstantin/django_polymorphic" rel="nofollow">some</a> <a href="http://code.google.com/p/django-polymorphic-models/" rel="nofollow">apps</a> and snippets trying to address this, but I don't find them very friendly to integrate.


Since Django uses Python, normal Python inheritance works. For more information about inheriting models, see the Django documentation about models, especially the section about <a href="https://docs.djangoproject.com/en/dev/topics/db/models/#model-inheritance" rel="nofollow">Model inheritance</a>.


  • How to Inherit objects in django?
  • Function notifies commentor when the poster should get notified
  • cancel plane ticket before 4 hours of departure time if dep time is 08:00:00 AM then Passengers can
  • Using the Django Formset to Generate a Dynamic Form from Multiple Models
  • enable or disable a field in maya ui with python
  • How to replace content_type and object_id fields by a field with actual object in admin inline?
  • Combine pairs to groups [PHP / Arrays]
  • Django - matching query does not exist
  • LINQ doesn't end with certain characters
  • SQL - How to find optimal performance numbers for query
  • Django “can't adapt type” error using mutliple databases (Postgresql 8.4 and sqlite3)
  • Enable Case Sensitive when using DataTable.select
  • Java Linked List How to create a node that holds a string and an int?
  • Entity Framework Adding Existing Item Causes Clone
  • Using raw SQL in django-rest framework?
  • Pandas: How to subset (and sum) top N observations within subcategories?
  • complex annotate on django query set
  • Mod rewrite redirection to another domain if file not exist
  • JDBI's @BindBean doesn't find named parameters in bean class during INSERT
  • Creating JS objects in PHP with commas in between
  • Xstream to map “choice” elements of XML
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • Cannot Parse HTML Data Using Android / JSOUP
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?