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
Am trying to hide fields on a serializer if the user does not have permission to access them. I think I saw somewhere that recommended doing this within the serializer's init function, so I wrote the following:
The problem being this doesn't pass the required context parameter. In fact the object gets constructed in the global scope before request is made available.
At one point I got confused with #2355, however I think this is unrelated.
Trouble is I can't actually think of a good solution to this :-(
Thanks
The text was updated successfully, but these errors were encountered:
.context is available during serialization and deserialization - you're correct that it won't be available during init for a nested serializer.
For this sort of use case I'd suggest either:
Customize get_serializer_class, return a different serializer depending on request.user.is_staff`.
Customize get_serializer and use that to dynamically alter the fields. eg...
serializer = super(ExampleView, self).get_serializer(_args, *_kwargs)
if ...:
del serializer.fields['albums']['revised']
del serializer.fields['albums']['revised_utc_offset']
return serializer
Hello,
Am trying to hide fields on a serializer if the user does not have permission to access them. I think I saw somewhere that recommended doing this within the serializer's init function, so I wrote the following:
This is nested in another serializer like as such:
The problem being this doesn't pass the required context parameter. In fact the object gets constructed in the global scope before request is made available.
At one point I got confused with #2355, however I think this is unrelated.
Trouble is I can't actually think of a good solution to this :-(
Thanks
The text was updated successfully, but these errors were encountered: