oxan / djangorestframework-dataclasses

Dataclasses serializer for Django REST framework
BSD 3-Clause "New" or "Revised" License
429 stars 28 forks source link

Converting serialized data back to dataclass #21

Closed errietta closed 4 years ago

errietta commented 4 years ago

Hi, given the following:

@dataclass
class Person():
  name: str

class PersonSerializer(DataclassSerializer):
  class Meta:
     dataclass = Person

When I have my json data from the client, I can serialize it with serialized_data = PersonSerializer(data={'name':'alice'}). However, if I call is_valid() and then validated_data, this will give me an OrderedDict, instead of a Person object. Am I missing something obvious, or do I just have to call Person(**serialized_data) to get it back to my Person dataclass?

oxan commented 4 years ago

Which version are you using? From 0.6 onwards validated_data should be an instance of the dataclass.

errietta commented 4 years ago

Yep, thanks @oxan I was on 0.4! I will update next time I have a weird problem 😂