In order to have a non-readonly PrimaryKeyRelatedField
, you are required to provide a queryset that contains valid options.
How can I properly populate that queryset based on the current request (user)?
In order to have a non-readonly PrimaryKeyRelatedField
, you are required to provide a queryset that contains valid options.
How can I properly populate that queryset based on the current request (user)?
View has a
attribute which you can then use to fetch user related queryset eg
The key is to subclass
PrimaryKeyRelatedField
and overload theget_queryset
method, using the user information from the request context:You can then use this new serializer just like the (unfiltered) original:
Whenever the serializer accesses the queryset, it will be filtered such that only objects owned by the current user are returned.