Permalink
Browse files

save owner

  • Loading branch information...
junkboy0315 committed Feb 11, 2019
1 parent 27ba16d commit c437c2d953ac638167c4d7983926309a16dd80f1
Showing with 10 additions and 1 deletion.
  1. +3 −0 tutorial/snippets/models.py
  2. +4 −1 tutorial/snippets/serializers.py
  3. +3 −0 tutorial/snippets/views.py
@@ -20,6 +20,9 @@ class Snippet(models.Model):
style = models.CharField(choices=STYLE_CHOICES,
default='friendly',
max_length=100)
owner = models.ForeignKey('auth.User',
related_name='snippets',
on_delete=models.CASCADE)
highlighted = models.TextField()

class Meta:
@@ -3,6 +3,9 @@


class SnippetSerializer(serializers.ModelSerializer):
owner = serializers.CharField(read_only=True)

class Meta:
model = Snippet
fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
fields = ('id', 'title', 'code', 'linenos',
'language', 'style', 'owner')
@@ -11,6 +11,9 @@ class SnippetList(generics.ListCreateAPIView):
queryset = Snippet.objects.all()
serializer_class = SnippetSerializer

# 新規作成(POST)時にはユーザ情報を一緒に保存する
def perform_create(self, serializer):
serializer.save(owner=self.request.user)

class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
"""

0 comments on commit c437c2d

Please sign in to comment.