카테고리 없음
2023-05-08 댓글 CRUD
jung1911
2023. 5. 9. 00:26
✅댓글
comment / models.py
from django.db import models
from users.models import User
class Comment(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
posts = models.ForeignKey(Post, on_delete=models.CASCADE)
content = models.TextField()
updated_at = models.DateTimeField(auto_now=True)
completion_at = models.DateTimeField(null=True, default=None, blank=True)
def __str__(self):
return str(self.title)
comment add/ admin.py
from django.contrib import admin
from comments.models import Comment
admin.site.register(Comment)
comment add/ serializers.py
from rest_framework import serializers
from comments.models import Comment
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = '__all__'
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = ("content",)
comment add/ urls.py
from django.urls import path
from comments import views
urlpatterns = [
path("<int:post_id>/comment", views.CommentView.as_view(),name='comment_view'),
path("<int:post_id>/comment/<int:comment_id>/", views.CommentDetailView.as_view(),name='comment_detail_view'),
]
comment add / views.py 틀잡기
# 댓글
class commentView(APIView):
def get(self, request, post_id):
pass
def post(self, request, post_id):
pass
# 댓글 수정 삭제
class CommnentDetailView(APIView):
def put(self, request):
pass
def del(self, request):
pass
comment add / views.py
from rest_framework.views import APIView
from rest_framework import status
from rest_framework.response import Response
from comments.models import Comment
from posts.models import Post
from comments.serializers import CommentSerializer,CommentCreateSerializer
from rest_framework.generics import get_object_or_404
# @@@@@@@@@@@@@@@@댓글@@@@@@@@@@@@@@@@
class CommentView(APIView):
def get(self, request, post_id):
posts = Post.objects.get(id=post_id)
comments = posts.comment_set.all() # 변수명 수정
serializer = CommentSerializer(comments, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
def post(self, request, post_id):
serializer = CommentCreateSerializer(data=request.data)
if serializer.is_valid():
serializer.save(user=request.user, post_id=post_id)
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status.HTTP_400_BAD_REQUEST)
#@@@@@@@@@@@@@@@@댓글 수정@@@@@@@@@@@@@@@@
class CommnentDetailView(APIView):
def put(self, request, post_id, comment_id):
comment = get_object_or_404(Comment, id=comment_id)
if request.user == comment.user:
serializer = CommentCreateSerializer(comment, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return Response("권한이 없습니다", status=status.HTTP_403_FORBIDDEN)
# @@@@@@@@@@@@@@@@댓글 삭제@@@@@@@@@@@@@@@@
def delete(self, request, post_id, comment_id):
comment = get_object_or_404(Comment, id=comment_id)
if request.user == comment.user:
comment.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
else:
return Response("권한이 없습니다", status=status.HTTP_403_FORBIDDEN)