diff --git a/website/snippets/serializers.py b/website/snippets/serializers.py index e69ff0a..b3d87c8 100644 --- a/website/snippets/serializers.py +++ b/website/snippets/serializers.py @@ -4,9 +4,10 @@ from rest_framework import serializers from .models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES class SnippetSerializer(serializers.ModelSerializer): + owner = serializers.ReadOnlyField(source='owner.username') class Meta: model = Snippet - fields = ['id', 'title', 'code', 'linenos', 'language', 'style'] + fields = ['id', 'title', 'owner', 'code', 'linenos', 'language', 'style'] class UserSerializer(serializers.ModelSerializer): snippets = serializers.PrimaryKeyRelatedField(many=True, queryset=Snippet.objects.all()) diff --git a/website/snippets/views.py b/website/snippets/views.py index 3a1c232..856c057 100644 --- a/website/snippets/views.py +++ b/website/snippets/views.py @@ -8,6 +8,9 @@ class SnippetList(generics.ListCreateAPIView): queryset = Snippet.objects.all() serializer_class = SnippetSerializer + def perform_create(self, serializer): + serializer.save(owner=self.request.user) + class SnippetDetail(generics.RetrieveUpdateDestroyAPIView): queryset = Snippet.objects.all()