In the last post we talked about Model Inheritance with regards to Abstract Model Classes, it was a great introduction into using OOP with your models to keep your code slim and tidy. You can read it at: Django Abstract Base Class - Model Inheritance
In this post we are going to discuss Multi-Table Inheritance. We will create a base model, that is not an abstract model, and inherit from the model. Then we are going to show you how to get and reference data from that models inheritance.
When we talk about multi-table inheritance we are referring to having a top level table with all the fields that are core to that context. Then we are going to have other tables with the more specific fields. You select from the table you want, and then do a join on a relationship to get the parent table data. Since Django does this through models and code, we will talk about it in those terms.
In our example we are going to have a Ticket Model.
class Ticket(models.Model): title = models.CharField(max_length=255, blank=True) description = models.TextField(blank=True) name = models.CharField(max_length=255, blank=True)
This model is a good piece of code because no matter what type of ticket we ever have we are going to have a title, description, and name. This is normal for any OOP we would do as a base class. Since we have this base model we can subclass it to have more specific models with data associated with them.
class InternalTicket(Ticket): user = models.ForeignKey('auth.User', null=True) class CustomerTicket(Ticket): product = models.TextField(blank=True)
Now we have 3 models available to us:
CustomerTicket. Each one has a
name as fields. However,
CustomerTicket does not have a
user field and
InternalTicket does not have
product field. Also both
CustomerTicket are stored in separate database table from
name. So if you looked up the tables in the database you would see a FK reference to the ticket table,
id field and the field you declared. Everything else lives in the ticket table.
With all that said lets look at creating some data and getting it back out. Because that is really when it makes sense on how it works.
In : CustomerTicket.objects.create(product='Videos', title='Odd Playback', description='doesnt play back fast enough', name='Jane Smith') Out: <CustomerTicket: CustomerTicket object>
We have created a
CustomerTicket object with all of our fields set, and it succeeds. (Further proof it works)
In : Ticket.objects.all() Out: [<Ticket: Ticket object>]
Here we are getting all tickets from the ticket model, which is including our CustomerTicket we create above.
In : ticket = Ticket.objects.get() In : ticket.customerticket Out: <CustomerTicket: CustomerTicket object>
If we reference the
customerticket property, which is the related name, it will return back the model so we can get the data from the model. We can look at that in action.
In : ticket.customerticket.product Out: 'Videos' In : ticket.description Out: 'doesnt play back fast enough'
Finally, you can see the description is still on the ticket level and not the cutomerticket model.
From the django docs:
multi-table inheritance uses an implicit OneToOneField to link the child and the parent
This means there is a one-to-one relationship between the ticket record and the corresponding subclassed models. Django just handles the translation of it from multiple tables to a single model so you don't really notice it.
As our final look we can look at how the
CustomerTicket object we created earlier references the ticket object.
n : customer = CustomerTicket.objects.get() In : customer.description Out: 'doesnt play back fast enough'
As you can see django just handles the fact that description is on the model, even though the description data is in the ticket table.
In : customer.ticket_ptr Out: <Ticket: Ticket object>
.ticket_ptr property is the reference to the ticket object so if you need to actually use that model you can.
In : customer.ticket_ptr_id Out: 1
ticket_ptr_id holds the id of the Ticket object in the database giving you another avenue for getting information out of the appropriate place.
You don't often need to use Multi-table inheritance, but when you do need to use it, it is nice that django makes it super simple to use. I have only seen a few instances where it makes sense, tickets being one, and social media posts as another. It provides great value in giving you core data, then when needed get the rest of the data. It does this while allowing you to use the models as separate models as if the inheritance didn't exist. Finally, another way to think of it is as data being used with polymorphism.