user association 2
This commit is contained in:
parent
e8cd3c33d2
commit
3632f27583
@ -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())
|
||||||
|
@ -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()
|
||||||
|
Reference in New Issue
Block a user