Skip to content

Inheritance

Model extension

Continuing with the user model example, let's imagine that we need to extend this model

from models_manager import Model, Field


class User(Model):
    id = Field(default=1, json='id')
    username = Field(default='some', json='username')
    email = Field(default='other', json='email')

So now we have new "User" object

{
  "id": 1,
  "username": "some",
  "email": "other",
  "token": "some-token",
  "tenant": {
    "id": 1,
    "name": "Customer"
  }
}

Let's upgrade our "User" model

from models_manager import Model, Field


class User(Model):
    id = Field(default=1, json='id')
    username = Field(default='some', json='username')
    email = Field(default='other', json='email')


tenant = {
    "id": 1,
    "name": "Customer"
}


class CustomerUser(User):
    token = Field(default='some-token', json='token')
    tenant = Field(default=tenant, json='tenant')

So now we can get "User" and "CustomerUser" objects json without duplicating same fields

from models_manager import Model, Field


class User(Model):
    id = Field(default=1, json='id')
    username = Field(default='some', json='username')
    email = Field(default='other', json='email')


tenant = {
    "id": 1,
    "name": "Customer"
}


class CustomerUser(User):
    token = Field(default='some-token', json='token')
    tenant = Field(default=tenant, json='tenant')

CustomerUser.manager.to_json

{
  "id": 1,
  "username": "some",
  "email": "other",
  "token": "some-token",
  "tenant": {
    "id": 1,
    "name": "Customer"
  }
}

For example, we need to override some field with new value

{
  "id": 1,
  "username": "another",
  "email": "other"
}

Let's do this

from models_manager import Model, Field


class User(Model):
    id = Field(default=1, json='id')
    username = Field(default='some', json='username')
    email = Field(default='other', json='email')


class SpecialUser(User):
    username = Field(default='another', json='username')


SpecialUser.manager.to_json

{
    "id": 1,
    "username": "another",
    "email": "other"
}

This way we can overwrite the attributes of the parent model

Field exclusion

Using the user as an example, let's look at how to exclude model fields

from models_manager import Model, Field


class ComplicatedUser(Model):
    id = Field(default=1, json='id', category=int)
    username = Field(default='some', json='username')
    email = Field(default='other', json='email')
    details = Field(default={}, json='details', category=dict)


class SimpleUser(ComplicatedUser):
    class Config:
        exclude_fields = ['details', 'email']

We created the ComplicatedUser model and inherited the SimpleUser model from it, in which the email, details fields were excluded. Let's now serialize the model and look at the dictionary

from models_manager import Model, Field


class ComplicatedUser(Model):
    id = Field(default=1, json='id', category=int)
    username = Field(default='some', json='username')
    email = Field(default='other', json='email')
    details = Field(default={}, json='details', category=dict)


class SimpleUser(ComplicatedUser):
    class Config:
        exclude_fields = ['details', 'email']


SimpleUser.manager.to_dict()
{'id': 1, 'username': 'some'}