oxan / djangorestframework-dataclasses

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

Ideas to a better way to instace of the dataclass instantiate_data_dictionaries #12

Closed Provoo closed 4 years ago

Provoo commented 4 years ago

Hi I red the coment below in instantiate_data_dictionaries:

# I'm not sure this is actually the best way to deserialize into nested dataclasses. The cleanest way seems to
        # be overriding to_internal_value(), but the top-level serializer must return a dictionary from that method. We
        # could split nested dataclasses off into a separate DataclassField (which could in general clean-up the code a
        # bit), but that breaks specifying nested serializers using a single class. Let's use this ugly hack until I can
        # think of something better.

First i want to congratulate you, with dataclasses django and python are becoming much better a robust to deploy productions services, I'm also front end developer and is amazing how with typescript and angular you can prevent a lot of bugs with de interfaces, I have been looking away to replacate that in backend, i tryed TypedDict, but my code steel been very dificult to debug, so when I started to use dataclasses and your library, the enpoint works like magic.

So I was wondering or I don't understand why you dont instance the main Dataclass instead of creating a Dict, if you want I can try to create this improvement.

Thanks again for your amazing work!

oxan commented 4 years ago

Thanks for your kind words!

I'm not exactly sure what you mean though. This is just an implementation detail, for the enduser this is completely opaque and they already get an instance of the main dataclass.

The problem hinted at here is that the location where that dataclass is instantiated. Ideally, I'd want to do that in to_internal_value(), so that nesting automatically works. However, DRF doesn't allow us to do that on the outermost serializer, so we have to do it somewhere else. I don't see what your suggestion to change that is.

oxan commented 4 years ago

encode/django-rest-framework#6053 and encode/django-rest-framework#6365 make me think this might not be intentional on DRF's side, so maybe I should look into why it doesn't work.