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>Answer2:
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.Answer3:
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>.