Serialization

Guide to overriding the `serialize()` method in the `Model` class to customize serialization.

The Model class includes a serialize() method that converts model instances into dictionaries.

The serialize() method is called when you pass a model instance to the Response object inside a view.

By default, serialize() includes all fields of the model or only the fields specified in serializable_fields. However, you might need to customize serialization to include additional information or change the format.

Quick Example

Here's a quick example of how you can override the serialize() method in a model class to customize serialization:

from blazingapi.orm.models import Model
from blazingapi.orm.fields import VarCharField, TextField

class Article(Model):

    serializable_fields = ['title', 'content']

    title = VarCharField(max_length=256)
    content = TextField()
    sensitive_data = VarCharField(max_length=256)

    def serialize(self):

        # Default serialization of specified fields
        serialized_data = super().serialize()

        # Add custom fields or transform existing ones
        serialized_data['summary'] = f"{self.content[:10]}..."

        return serialized_data


article = Article(title='Hello', content='This is a long article...', sensitive_data='Secret')

print(article.serialize())

The output of article.serialize() will be a dict object with the following structure:

{
    "title": "Hello",
    "content": "This is a long article...",
    "summary": "This is a ..."
}

In this example, the sensitive_data field is not included in the serializable_fields list, so it won't be serialized by super().serialize().

Also, the serialize() method is overridden to include a custom summary field that contains the first 10 characters of the content field.