from dataclasses import dataclass
from rest_framework.fields import CharField
from rest_framework_dataclasses.serializers import DataclassSerializer
@dataclass
class Foo:
bar: str
class ASerializer(DataclassSerializer):
class Meta:
dataclass = Foo
fields = ("renamed_bar",)
renamed_bar = CharField(source="bar")
ser = ASerializer(data={"renamed_bar": "string"})
ser.is_valid(raise_exception=True)
foo = ser.create(ser.validated_data)
This code does not raise on is_valid(raise_exception=True) but fails with KeyError when calling create.
Here is a minimal example:
This code does not raise on
is_valid(raise_exception=True)
but fails withKeyError
when callingcreate
.