I have a serializer that accepts two objects, one of which is always constant once a user has authenticated.
The serializer, of course, should not need to know these details; it should know only that it serializes and deserializes an object with two fields.
Further, the user, having authenticated, should only have to pass in the value he wishes to set to the non-constant field via the viewset.
So what I need is a hook somewhere that intercepts the data before the serializer receives it, sets the constant field to the user's set value, and then allows processing to proceed as usual.
Is there any such hook in DRF? The solutions I've seen are all very hacky: context (coupling of view and serialization logic); validations (more coupling); and so forth. In theory what we want is:
data = union(userFields, fixedFieldsForView)
Serializer(data=data)
where the data
part is somehow handled in the view only. Note also this is only during creation and update of objects, not reads (which are filtered already by get_queryset
).