user association 2

This commit is contained in:
Pratyush Desai 2022-01-08 05:13:06 +05:30
parent e8cd3c33d2
commit 3632f27583
Signed by: pratyush
GPG Key ID: DBA5BB7505946FAD
2 changed files with 5 additions and 1 deletions

View File

@ -4,9 +4,10 @@ from rest_framework import serializers
from .models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES from .models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES
class SnippetSerializer(serializers.ModelSerializer): class SnippetSerializer(serializers.ModelSerializer):
owner = serializers.ReadOnlyField(source='owner.username')
class Meta: class Meta:
model = Snippet model = Snippet
fields = ['id', 'title', 'code', 'linenos', 'language', 'style'] fields = ['id', 'title', 'owner', 'code', 'linenos', 'language', 'style']
class UserSerializer(serializers.ModelSerializer): class UserSerializer(serializers.ModelSerializer):
snippets = serializers.PrimaryKeyRelatedField(many=True, queryset=Snippet.objects.all()) snippets = serializers.PrimaryKeyRelatedField(many=True, queryset=Snippet.objects.all())

View File

@ -8,6 +8,9 @@ class SnippetList(generics.ListCreateAPIView):
queryset = Snippet.objects.all() queryset = Snippet.objects.all()
serializer_class = SnippetSerializer serializer_class = SnippetSerializer
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
class SnippetDetail(generics.RetrieveUpdateDestroyAPIView): class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Snippet.objects.all() queryset = Snippet.objects.all()