oxan / djangorestframework-dataclasses

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

Acessing optional serialized fields yields `rest_framework.fields.empty`. #27

Closed wkrueger closed 3 years ago

wkrueger commented 3 years ago
@dataclass
class SomeClass:
    maybeDog: Optional[Dog]

instance = getInst() # lets say getInst() serializes an input with that dataclass, gets value through validated_data
if instance.maybeDog:  # this is always truthy, because when maybeDog is absent its value is acessed as field.empty
    print('bark')
oxan commented 3 years ago

See this note in the changelog. Use save() if you want the default field value.

oxan commented 3 years ago

This issue actually made me think of a way to avoid exposing the empty sentinel, which I implemented in f3fff475.