Created
August 3, 2015 14:53
-
-
Save SafPlusPlus/57877bea91bd7533f62e to your computer and use it in GitHub Desktop.
scoped HyperlinkRelatedField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ScopedHyperlinkedRelatedField(serializers.HyperlinkedRelatedField): | |
""" A subclass of the HyperlinkRelatedField which attempts to scope on provider/zone """ | |
def get_queryset(self): | |
user = self.context['request'].user | |
if user.is_superuser: | |
queryset = self.queryset | |
else: | |
queryset = self.queryset.filter(zone__provider__groups__user=user) | |
if isinstance(queryset, (QuerySet, Manager)): | |
queryset = queryset.all() | |
return queryset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment