You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've recently stumbled into a simliar pattern twice where I want to configure a list such that one can pass in partial objects that only contain a primary key, but have fleshed out objects in the response. In the example below we're creating (or PUT-ing) an object that has relations to already existing entities, and we don't want to allow the client to alter the related entities in this request, but we want the client to conveniently be able to get the full objects in the response.
For lack of a better name, I'm calling this an "augmented list" for now. Suggestions are welcome.
classAugmentedList(ListField):
def__init__(self, child: Serializer, method_name: str|None=None, writable_field: str="id") ->None:
super().__init__(source="*", child=child, default=list)
self.method_name=method_nameself.writable_field=writable_fielddefto_internal_value( # type: ignore[override]self, data: Iterable[dict[str, int]]
) ->dict[str, Iterable[int]]:
assertself.field_nameisnotNonereturn {self.field_name: [item[self.writable_field] foritemindata]}
# Stolen from SerializerMethodFielddefbind(self, field_name, parent):
# The method name defaults to `get_{field_name}`.ifself.method_nameisNone:
self.method_name='get_{field_name}'.format(field_name=field_name)
super().bind(field_name, parent)
# Stolen from SerializerMethodField and modified to use self.child for serialization and to not pass# any value to the method.defto_representation(self, value):
method=getattr(self.parent, self.method_name)
return [
type(self.child)(instance=child_instance).dataforchild_instanceinmethod()
]
The above specialized list field allows us to achieve the behavior we want, and would be used like this:
I've recently stumbled into a simliar pattern twice where I want to configure a list such that one can pass in partial objects that only contain a primary key, but have fleshed out objects in the response. In the example below we're creating (or PUT-ing) an object that has relations to already existing entities, and we don't want to allow the client to alter the related entities in this request, but we want the client to conveniently be able to get the full objects in the response.
Example request
Example response
Solution
For lack of a better name, I'm calling this an "augmented list" for now. Suggestions are welcome.
The above specialized list field allows us to achieve the behavior we want, and would be used like this:
(The above code is untested as-is, but all the concepts are fully working in separate places at the moment).
The text was updated successfully, but these errors were encountered: