[Django]-Django: composite fields or embedded classes (like JPA)?

5πŸ‘

βœ…

I think you might be over-thinking inheritance. Inheritance is and is actually the recommended method to composite models. The following is an example of how properly use model inheritance in Django:

class PhoneModelBase(model.Model):
    phone = models.CharField(max_length=16)
    ...

    class Meta:
        abstract = True

class PhoneModel(PhoneModelBase):
    # phone is here without typing it
    # the only restriction is that you cannot redefine phone here
    # its a Django restriction, not Python restriction
    # phone = models.CharField(max_length=12) # <= This will raise exception
    pass

So what this does is it creates a model PhoneModelBase, however the key here is that it uses class Meta with abstract=True.

Here is more of behind the scenes of what is going on and some explanation of some of the Python concepts. I assume that you are not aware of them since you mentioned Java in the question. These Python concepts are actually rather confusing concepts so my explanation is probably not full, or even confusing, so if you will not follow, don’t mind. All you have to know is to use abstact = True. Here is the official doc: https://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes.

Meta attribute within PhoneModelBase is just that, an attribute. It is the same as any other attribute within the class, except its a class instance (remember that in Python classes and functions are first order members). In addition, Python has this thing called __metaclass__ which you can add to you classes. __metaclass__ defines a way of how an instance of a class is build. More about that here. Django uses these in how it creates model class instances.

So to create the PhoneModelBase class, the following is a rough outline:

  • When an instance of the PhoneModelBase class (the class itself, not instance of class – PhoneModelBase()) is being created, the __metaclass__ which comes from model.Model due to inheritance takes over the creation process
  • Within the __metaclass__, Python calls the function which creates the actual class instance and passes to it all of the fields from the class you are trying to create – PhoneModelBase. That will include phone, Meta and any other fields you define
  • It sees the Meta attribute and then it starts analyzing its attributes. According to the values of these attributes, Django will change the behavior of the model
  • It sees the abstract attribute and then changes the logic of the class its trying to create – PhoneModelBase by not storing it in db

So then the PhoneModelBase, even though its definition looks very similar to a regular model, its not a regular model. It is just an abstract class which is meant to be used as composite in other models.

When other models inherit from PhoneModelBase, their __metaclass__ will copy the attributes from the base model as if you manually typed those attributes. It will not be a foreign key on anything like that. All of the inherited attributes are going to be a part of the model and will be in the same table.

Hopefully all of this makes some sense. If not, all you have to remember is just to use Meta class with abstract = True.

EDIT

As suggested in the comment, you can also inherit from multiple base classes. So you can have PhoneModelBase, DimensionsModelBase, and then you can inherit from both of these (or more), and all of the attributes from all base classes will be present in your model.

πŸ‘€miki725

Leave a comment