-
-
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
[Feature request] Use typing.Annotated
to redefine field types
#92
Comments
I don't think this has anything to do with
It's certainly possible, but I think the more important question is whether it's desirable. There's already support for specifying the serializer field type in the dataclass definition like this: @dataclass
class UploadDocumentRequest:
file: InMemoryUploadedFile = dataclasses.field(metadata={'serializer_field': fields.FileField()}) I suppose it's a bit more verbose, but it's also more generic: you can also add a |
It is also possible with @dataclasses.dataclass
class Person:
# email: str = dataclasses.field(metadata={'serializer_field': fields.EmailField()})
email: Annotated[str, fields.EmailField()]
# age: int = dataclasses.field(metadata={'serializer_kwargs': {'min_value': 0}})
age: Annotated[int, fields.IntegerField(min_value=0)] |
Ah yeah, that's true, though it's a bit more limited, as it requires you to specify all the required arguments of the field, instead of just the ones you want to override. My point remains standing though, we already have a way to override fields within the dataclass declaration, and I don't see a good reason to add another one (yet). |
In my case, I have a common type that used between couple serializers: DocumentsType: TypeAlias = Annotated[
list[models.Document],
PrimaryKeyRelatedField(
queryset=models.Document.objects.filter(deleted_at=None),
many=True,
),
]
@dataclass
class A:
documents: DocumentsType
@dataclass
class B:
documents: DocumentsType
|
Hi, thanks for the amazing project :)
I'm having function like this to create serializers inline:
So now I can create serializers from dataclasses that looks like this:
Without checking for Annotated types, I got this error:
I was wondering if it's possible to implement discovery of fields from
Annotated[...]
on your side.The text was updated successfully, but these errors were encountered: