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 | |
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 | |
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#
ModelSerializeruses your model's field types and default values for validation.Config.modelandConfig.fieldsare required forModelSerializer.- Use
Config.required_fieldsto force fields to be required. - Use
Config.optional_fieldsto force fields to be optional. - A field cannot be in both
required_fieldsandoptional_fields. - If you use
required_fieldsoroptional_fields, those fields must also be listed infields. - You can use
'*'forfields,required_fields, oroptional_fieldsto include all model fields. Config.excludeis useful whenfieldsis set to'*'.- You can add custom fields and validators when combining
ModelSerializerwith Pydantic features.
File Handling in Serializers#
When working with file uploads, Panther's File and Image classes integrate seamlessly with serializers.
Comprehensive File Handling Guide
For detailed information about file handling, including advanced features, best practices, and troubleshooting, see the dedicated File Handling documentation.
Basic File Serialization#
| app/serializers.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
File Validation#
| app/serializers.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
File Properties#
When working with File objects in serializers, you have access to:
file.file_name: The original filenamefile.content_type: The MIME typefile.size: File size in bytesfile.file: The file content as bytes