-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DataclassSerializer fails with source="*"
embedded serialiser fields because it requires Meta.dataclass
#77
Comments
I have submitted a couple of options to solve the problem. Please have a look and see which solution works best for you. |
Is there any reason why you need your inner serializer ( |
So not sure, but if you check, both solutions differ in exactly that, one
uses the DataclassSerializer and the other the regular one. Right now both
fail.
It was mainly failing because of the requirements for getting the embedded
structure fields.
Feel free to remove the patch on the serialisers.py file to see how it
fails.
…On Fri, 28 Apr 2023, 20:15 Oxan van Leeuwen, ***@***.***> wrote:
Is there any reason why you need your inner serializer (
PetInformationSerializer) to subclass from DataclassSerializer? The whole
thing of DataclassSerializer is that it autogenerates the serializer
fields for you, but if you suppress that (like your PRs do), I don't see
why you cannot use a regular Serializer instead (and that indeed seems to
work fine for your example).
—
Reply to this email directly, view it on GitHub
<#77 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABXXGT3RLIHOTTVDQHSC5DXDQCKTANCNFSM6AAAAAAXLKWNMY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Maybe I'm overlooking something, but with vanilla drf-dataclasses the below standalone script seems to work as it should to me: from rest_framework_dataclasses.serializers import DataclassSerializer
from rest_framework import fields, serializers
from typing import Literal
import dataclasses
@dataclasses.dataclass
class Pet:
animal: Literal['cat', 'dog']
name: str
class TestSerializer(DataclassSerializer):
class NestedSerializer(serializers.Serializer):
animal = fields.CharField()
class Meta:
dataclass = Pet
fields = ['name', 'information']
name = fields.CharField()
information = NestedSerializer(source='*')
ser = TestSerializer(instance=Pet(animal='cat', name='Milo'))
print(ser.data) # {'name': 'Milo', 'information': OrderedDict([('animal', 'cat')])}
dser = TestSerializer(data={'name': 'Milo', 'information': {'animal': 'dog'}})
dser.is_valid(raise_exception=True)
print(dser.validated_data) # Pet(animal='dog', name='Milo') |
Hello Oxan,
Yes you are right, it was an oversight from my side, sorry for that. I have
tested it through, and the only issue that arises when using
DataclassSerializer is that this fails if you don't put the `fields` field
in the `Meta`.
Cheers, Javier
…On Fri, Apr 28, 2023 at 9:18 PM Oxan van Leeuwen ***@***.***> wrote:
Maybe I'm overlooking something, but with vanilla drf-dataclasses the
below standalone script seems to work as it should to me:
from rest_framework_dataclasses.serializers import DataclassSerializerfrom rest_framework import fields, serializersfrom typing import Literalimport dataclasses
@dataclasses.dataclassclass Pet:
animal: Literal['cat', 'dog']
name: str
class TestSerializer(DataclassSerializer):
class NestedSerializer(serializers.Serializer):
animal = fields.CharField()
class Meta:
dataclass = Pet
fields = ['name', 'information']
name = fields.CharField()
information = NestedSerializer(source='*')
ser = TestSerializer(instance=Pet(animal='cat', name='Milo'))print(ser.data) # {'name': 'Milo', 'information': OrderedDict([('animal', 'cat')])}
dser = TestSerializer(data={'name': 'Milo', 'information': {'animal': 'dog'}})dser.is_valid(raise_exception=True)print(dser.validated_data) # Pet(animal='dog', name='Milo')
—
Reply to this email directly, view it on GitHub
<#77 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABXXGU4MT4HBZC5BFNIUJDXDQJZ3ANCNFSM6AAAAAAXLKWNMY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
OK, I have refactored the PR to showcase the missing usecase, where an embedded dataclass fails to build because of the
|
Ah, that's a good example. It's a tricky problem to solve though. On deserialization, normally each |
According to https://www.django-rest-framework.org/api-guide/fields/#using-source, the serialisers should allow for use of
source="*"
.The issue can be reproduced in the following way. Copy the following snippet on the bottom of
tests/test_functional.py
:The text was updated successfully, but these errors were encountered: