Browse Source

Add filter for serializer

pull/10/head
Hironsan 6 years ago
parent
commit
44d4eba568
1 changed files with 13 additions and 2 deletions
  1. 15
      app/server/serializers.py

15
app/server/serializers.py

@ -18,8 +18,19 @@ class TextSerializer(serializers.ModelSerializer):
fields = ('id', 'text')
class ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
def get_queryset(self):
view = self.context.get('view', None)
request = self.context.get('request', None)
queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()
if not request or not queryset or not view:
return None
return queryset.filter(project=view.kwargs['project_id'])
class DocumentAnnotationSerializer(serializers.ModelSerializer):
label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
class Meta:
model = DocumentAnnotation
@ -39,7 +50,7 @@ class DocumentSerializer(serializers.ModelSerializer):
class SequenceAnnotationSerializer(serializers.ModelSerializer):
label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
class Meta:
model = SequenceAnnotation

Loading…
Cancel
Save