{"id":3467,"library":"djangorestframework-dataclasses","title":"djangorestframework-dataclasses","description":"djangorestframework-dataclasses (current version 1.4.0) is an active Python library providing a dataclasses serializer for Django REST Framework (DRF). It offers automatic field generation for Python dataclasses, mirroring the functionality of DRF's `ModelSerializer` for Django models, making it easier to define API schemas using dataclasses. The library is actively maintained with regular releases.","status":"active","version":"1.4.0","language":"en","source_language":"en","source_url":"https://github.com/oxan/djangorestframework-dataclasses","tags":["django","rest-framework","dataclasses","serialization","api"],"install":[{"cmd":"pip install djangorestframework-dataclasses","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for Django integration.","package":"django","optional":false},{"reason":"The library extends Django REST Framework serializers.","package":"djangorestframework","optional":false},{"reason":"Required for older Python versions (less than 3.8) to support certain typing features.","package":"typing_extensions","optional":true}],"imports":[{"symbol":"DataclassSerializer","correct":"from rest_framework_dataclasses.serializers import DataclassSerializer"}],"quickstart":{"code":"from dataclasses import dataclass\nimport datetime\nfrom typing import Optional\n\nfrom rest_framework import fields, serializers\nfrom rest_framework_dataclasses.serializers import DataclassSerializer\n\n@dataclass\nclass UserProfile:\n    username: str\n    email: str\n    is_active: bool = True\n    date_joined: Optional[datetime.datetime] = None\n\nclass UserProfileSerializer(DataclassSerializer):\n    class Meta:\n        dataclass = UserProfile\n        fields = '__all__'\n\n# Example Usage:\n# Serialization\nuser_instance = UserProfile(\n    username='testuser',\n    email='test@example.com',\n    date_joined=datetime.datetime.now(datetime.timezone.utc)\n)\nserializer = UserProfileSerializer(user_instance)\nprint(\"Serialized data:\", serializer.data)\n\n# Deserialization\ndata = {\n    'username': 'newuser',\n    'email': 'new@example.com'\n}\ndeserializer = UserProfileSerializer(data=data)\ndeserializer.is_valid(raise_exception=True)\nnew_user_profile = deserializer.validated_data\nprint(\"Deserialized object:\", new_user_profile)\nprint(\"Deserialized username:\", new_user_profile.username)\nprint(\"Deserialized is_active (with default):\", new_user_profile.is_active)\n","lang":"python","description":"This quickstart demonstrates how to define a dataclass, create a `DataclassSerializer` for it, and then use the serializer to convert a dataclass instance into a dictionary (serialization) and to create a dataclass instance from a dictionary (deserialization), including handling default values."},"warnings":[{"fix":"Upgrade `mypy` to version 1.0 or newer in your development environment or CI/CD pipelines.","message":"Type annotations in `djangorestframework-dataclasses` versions 1.3.0 and newer require `mypy` 1.0 or higher for correct validation. Older `mypy` versions may produce incorrect results or errors.","severity":"breaking","affected_versions":">= 1.3.0"},{"fix":"Ensure fields intended to be optional have a default value (e.g., `field: str = ''` or `field: Optional[str] = None`) or `default_factory`. For explicit control, use `extra_kwargs={'field_name': {'required': False}}` in the `Meta` class.","message":"In versions 0.9.0 and later, dataclass fields with a default value or `default_factory` are automatically marked as optional (`required=False`) in the serializer. Marking a field with `typing.Optional` now only makes it nullable, not optional. If a field previously relied solely on `typing.Optional` to be non-required, it will now be considered required if it doesn't have a default value.","severity":"breaking","affected_versions":">= 0.9.0"},{"fix":"When targeting Python 3.10 and newer, use `FieldType | None` instead of `typing.Optional[FieldType]` for optional fields in your dataclass definitions.","message":"As of v1.1.0, `djangorestframework-dataclasses` supports the new `X | None` union syntax (PEP 604) for specifying optional fields in Python 3.10+. This is the preferred modern way to declare optional fields.","severity":"gotcha","affected_versions":">= 1.1.0 (for Python 3.10+)"},{"fix":"Modify code to check for the absence of a key in `validated_data` using `if 'key' not in validated_data:` instead of checking for `value is rest_framework.fields.empty`.","message":"The `validated_data` representation no longer contains the `rest_framework.fields.empty` sentinel value for unsupplied fields since v0.8. This change reverted a breaking behavior introduced in v0.7. Code relying on the presence of `empty` for unsupplied fields will need adjustment.","severity":"gotcha","affected_versions":">= 0.8.0"},{"fix":"Review serialization/deserialization logic for custom or less common composite types. If specific serialization behavior is needed, use the `serializer_field_mapping` dictionary in the serializer's `Meta` class to override the field for those types.","message":"With v1.3.0, values for fields of non-list/dict composite types (e.g., `frozenset`, `OrderedDict`) are now created as their specific composite type, rather than always `list` or `dict`. This provides more accurate type handling but might affect existing code if it implicitly relied on the previous generic behavior.","severity":"gotcha","affected_versions":">= 1.3.0"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}