From 3632f275837daeaac2ab9565606a8450902f20b8 Mon Sep 17 00:00:00 2001 From: Pratyush Desai Date: Sat, 8 Jan 2022 05:13:06 +0530 Subject: [PATCH] user association 2 --- website/snippets/serializers.py | 3 ++- website/snippets/views.py | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) 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()