Created
June 14, 2013 10:01
-
-
Save chiller/5780768 to your computer and use it in GitHub Desktop.
filtering comments on user
This file contains hidden or 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 CommentRelatedField(serializers.ManyRelatedField): | |
def to_native(self, value): | |
return CommentSerializer(value).data | |
def field_to_native(self, obj, field_name): | |
try: | |
if self.source == '*': | |
return self.to_native(obj) | |
source = self.source or field_name | |
value = obj | |
for component in source.split('.'): | |
value = get_component(value, component) | |
if value is None: | |
break | |
except ObjectDoesNotExist: | |
return None | |
if value is None: | |
return None | |
if self.context['request'].user.groups.filter(name="Internal"): | |
qs = value.all() | |
else: | |
qs = value.exclude(user__groups__name="Internal") | |
if self.many: | |
return [self.to_native(item) for item in qs] | |
return self.to_native(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment