Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Dynamically limiting queryset of related field

class UserPKField(serializers.PrimaryKeyRelatedField):
    def get_queryset(self):
        user = self.context['request'].user
        queryset = User.objects.filter(...)
        return queryset

class PurchaseSeriaizer(serializers.ModelSerializer):
    users = UserPKField(many=True)

    class Meta:
        model = Purchase
        fields = ('id', 'users')
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Dynamically #limiting #queryset #related #field
ADD COMMENT
Topic
Name
3+5 =