oxan/djangorestframework-dataclasses

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

wkrueger opened this issue · 2 comments

@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

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

oxan commented

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