Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

dynamically created queryset for PrimaryKeyRelatedField in drf

class UserFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
    def get_queryset(self):
        request = self.context.get('request', None)
        queryset = super(UserFilteredPrimaryKeyRelatedField, self).get_queryset()
        if not request or not queryset:
            return None
        return queryset.filter(user=request.user)
      

class MySerializer(serializers.ModelSerializer):
    related = UserFilteredPrimaryKeyRelatedField(queryset=MyModel.objects)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #dynamically #created #queryset #PrimaryKeyRelatedField #drf
ADD COMMENT
Topic
Name
4+2 =