dabapps / django-readers

A lightweight function-oriented toolkit for better organisation of business logic and efficient selection and projection of data in Django projects.
https://www.django-readers.org
BSD 2-Clause "Simplified" License
183 stars 7 forks source link

Override keyword args for automatically generated serializer fields #99

Closed j4mie closed 2 months ago

j4mie commented 2 months ago

Fixes #98

This adds optional keyword arguments to the out object (when used as either a decorator or using the >> approach) which are passed to the constructor of the serializer field. This provides a way to add useful attributes to the field (like label and help_text) without throwing away the automagic model introspection behaviour.

class SomeView(SpecMixin, RetrieveAPIView):
    queryset = SomeModel.objects.all()
    spec = [
        ...,
        "title" >> out(label="The title of the object")
        ...,
    ]

Thanks @carltongibson for the API suggestion.

j4mie commented 2 months ago

Just calling out that this does change an internal API: previously, the serializer field was added to an attribute named out, now we have a generic _readers_annotation dictionary. This is undocumented behaviour so does not require a major version bump.