Meta inner class in Django: abstract = True

Meta inner class in Django: abstract = True

Interspersed images of coding and of hands in neon pinks and purples. Text on top of pictures. Text reads: Meta inner class in Django: abstract = true. Allows us to create superclasses to inherit from without actually storing them in our databases.
The most magical thing that I learned today. Thank you. 😍
Today I learned what a Meta inner class in Django is.

This one is a bit tricky so please allow me to explain.


Let's say I want to create a Model (or a table) in Django.


I want it to be a table that inherits from a certain other table.


However, I do not want the said original table to exist - because I don't actually need to use it - and it will take up memory when I store it. So let's give an example.


I want to create a table for dragons. However I have two types of dragons I want to store data on - Hungarian dragons and Welsh dragons.


Let's say I want to store all of the data on those - and they have some separate characteristics that means that I need to store both of these different types in two separate types of tables.


Two types of dragons with different characteristics needing information stored on them - two different types of tables with different data fields.


HOWEVER, they have enough in common that it makes sense for them to inherit from a common table in the first instance.


So let's say I want to create a table for each one of them - but they both have unique characteristics so they need their own unique tables.


So therefore - I want to create an overarching dragons table that THEY WILL BOTH INHERIT FROM. However, I do not actually NEED TO USE THAT DRAGONS TABLE ON ITS OWN AND IN ITS OWN RIGHT AND FOR ITS OWN PURPOSE AND BY ITSELF for anything.


Therefore: I MIGHT DO THIS:


class Dragons (models.Model):

name = models.CharField(max_length=100)

colour = models.CharField(max_length=100)


class Meta:

abstract = True


THEREFORE THIS MEANS THAT the model "Dragons" doesn't actually get saved in the database.


It just gets passed on to the following inherited classes, like so:


class HungarianDragon(Dragons) =

scale_thickness = models.IntegerField()

tail_length = models.IntegerField()


class WelshDragon(Dragons) =

wing_span = models.IntegerField()

fire_breath_temperature = models.IntegerField()


Like this, the `HungarianDragon` and `WelshDragon` tables actually get created to our database.


But the `Dragons` table does not actually get created - it is only abstract - and therefore it doesn't take up space in our memory.


Thus the 'abstract' table is like an imaginary pretend table that we can actually inherit from without having to create it in the database.


And this can be done by adding in the subclass


class Meta:

abstract = True


into our main class.


And this allows us to build more advanced, more complex and more nuanced tables, on top of this, as well. It makes our code more SCALABLE as it means that we can repeat things more quickly.

And this is all what I learned from a Senior colleague today.

I have the most amazing, wonderful, brilliant Senior colleagues in the world. πŸ’šπŸ’œ


Thank you. πŸ’™





Comments

Popular Posts