Panther Serializers#
Panther provides flexible ways to define serializers for your APIs. Serializers are responsible for validating and transforming input data. This guide covers the three supported styles and when to use each.
Introduction#
serializer in Panther is a class that defines how input data is validated and (optionally) transformed before being processed by your API logic. Panther supports three main styles:
-
Pydantic Serializer: Use a standard Pydantic model.
-
ModelSerializer: Generate fields from a Panther model.
-
ModelSerializer + Pydantic: Combine model-based fields with Pydantic features and custom validation.
Style 1: Pydantic Serializer#
Use a regular Pydantic class as your serializer. This is the most direct approach and is ideal for simple use cases or when you want full control over the fields.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Style 2: ModelSerializer#
Use Panther's ModelSerializer
to automatically generate serializer fields from your model. This is useful for DRY code and consistency between your models and serializers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Style 3: ModelSerializer with Pydantic Features#
Combine ModelSerializer
with Pydantic features for advanced use cases. This allows you to add custom fields, validators, and configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Comparison Table#
Feature | Pydantic Serializer | ModelSerializer | ModelSerializer + Pydantic |
---|---|---|---|
Model-based fields | ❌ | ✅ | ✅ |
Custom fields | ✅ | ❌ | ✅ |
Pydantic validators | ✅ | ❌ | ✅ |
Field inclusion/exclude | Manual | Configurable | Configurable |
Best for | Simple cases | DRY, model-aligned | Advanced/Hybrid |
Notes & Best Practices#
ModelSerializer
uses your model's field types and default values for validation.Config.model
andConfig.fields
are required forModelSerializer
.- Use
Config.required_fields
to force fields to be required. - Use
Config.optional_fields
to force fields to be optional. - A field cannot be in both
required_fields
andoptional_fields
. - If you use
required_fields
oroptional_fields
, those fields must also be listed infields
. - You can use
'*'
forfields
,required_fields
, oroptional_fields
to include all model fields. Config.exclude
is useful whenfields
is set to'*'
.- You can add custom fields and validators when combining
ModelSerializer
with Pydantic features.