Polymorphism in Python and Django

What is polymorphism?

Polymorphism is the ability of an object to take on many forms.

When is polymorphism used?

A polymorphic model is used when a single entity requires different functionality or information.

Difference of Polymorphic and Abstract Classes

Simply put, abstract instances do not exist. An abstract class is just a way of generalizing information for future children classes. Therefore, the only objects you’ll be able to relate are the children instances, never the parent abstract class.

If you want to relate to the parent abstract class, this is where polymorphic classes come in handy in Django.

I’m currently working on a project where I need classes related with a OneToMany relationship with that parent abstract class. If you relate it without polymorphism you’ll get the following error:

Django Polymorphic models

The good news is that it’s actually really straightforward to use polymorphism in Django! I’ve used the library django-polymorphic

Step 1: Install the library

Step 2: Update the settings.py file

Step 3: Define your polymorphic model

Step 4: Define your polymorphic children models

Step 5: Relate the polymorphic model to other models

Step 6: Query the models in the view

You can check what kind of object is the block you get. So for instance:

You can also use instance_of or not_instance_of for narrowing the result to specific subtypes:

Final notes

Taking this same example, how does this really work in the backend? When you’re getting a BlockOne or a BlockTwo instance, internally Django will have to perform an INNER JOIN to get all the parent’s data. So, as the documentation says, taking performance into account “While django-polymorphic makes subclassed models easy to use in Django, we still encourage to use them with caution

Resources:

django-polymorphic library

realpython

Happy codding! :)

Coder, Entrepreneur, Co-founder at SlowCode