Back to Posthog

Quick Reference — "I See X, Do Y"

.agents/skills/improving-drf-endpoints/references/quick-reference-table.md

1.43.13.9 KB
Original Source

Quick Reference — "I See X, Do Y"

You see thisDo thisDetails
serializers.JSONField()Create custom field class + @extend_schema_field(PydanticModel)serializer-fields.md
serializers.ListField() without child=Add child=serializers.CharField() (or appropriate type)serializer-fields.md
serializers.DictField() without child=Add child= with typed valuesserializer-fields.md
Any field without help_textAdd help_text="..." describing purpose, format, constraintsserializer-fields.md
CharField for a fixed set of valuesChange to ChoiceField(choices=[...])serializer-fields.md
SerializerMethodField without annotationAdd @extend_schema_field(...) on its get_* methodserializer-fields.md
request: ValidatedRequest with typed DTOUse TypedRequest[MyDTO] for type-safe validated_dataviewset-annotations.md
Plain ViewSet with serializer.is_valid()Replace with @validated_request decoratorviewset-annotations.md
@action without @extend_schemaAdd @extend_schema(request=..., responses=...) above @actionviewset-annotations.md
@extend_schema on class (not method)Move to the actual handler method (get, post, create, etc.)viewset-annotations.md
responses={400: OpenApiTypes.OBJECT}Use OpenApiResponse(description="...")viewset-annotations.md
Same serializer for read + write with read_only fieldsSplit into separate read/write serializersviewset-annotations.md
Custom @action returning non-paginated dataSet pagination_class=None on the actionviewset-annotations.md
fields = "__all__" on ModelSerializerExplicitly list fields — __all__ leaks internal fields to the APIGeneral DRF practice
Endpoint returns raw dict/list (no response serializer)Create a response serializer so downstream types are generatedviewset-annotations.md
Streaming/SSE endpointUse @extend_schema(request=InputSerializer, responses={(200, "text/event-stream"): OpenApiTypes.STR})viewset-annotations.md