drf-pydantic is a Python library that enables seamless integration of Pydantic models with the Django REST Framework (DRF). It allows developers to use Pydantic for data validation and serialization within DRF serializers, leveraging Pydantic's robust schema definition and validation capabilities. The current version is 2.9.1, and it typically follows a release cadence tied to Django, DRF, or Pydantic updates, with minor releases for features and bug fixes.
pip install drf-pydanticVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a Pydantic model, create a `PydanticSerializer` for it, and then use it within a DRF `APIView` for both validating incoming data (POST) and serializing outgoing data (GET). The validated data from the serializer is a Pydantic model instance.
Rewrite serializers using `PydanticSerializer` or `PydanticModelSerializer` and their `Meta` classes. Consult the official documentation for v2.x migration.
Ensure you are using the correct serializer for your Pydantic model type. Use `PydanticSerializer` for standalone Pydantic models. Use `PydanticModelSerializer` only when your Pydantic model is designed to map directly to a Django ORM model.
Always include `class Meta: model = YourPydanticModel` where `YourPydanticModel` is your Pydantic `BaseModel` or Django-derived Pydantic model.