Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comment_create(mseo39) #40

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions pet_promotion/comment/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
from django.contrib import admin
from .models import Comment
# Register your models here.
admin.site.register(Comment)
# Register your models here.
10 changes: 2 additions & 8 deletions pet_promotion/comment/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
from django.db import models
from board.models import Post

# Create your models here.

class Comment(models.Model):
comment = models.TextField()
create_date = models.DateField(auto_now_add=True)
update_date = models.DateField(auto_now_add=True)
post = models.ForeignKey(Post, blank=False, null=False, on_delete=models.CASCADE)
user = models.ForeignKey("user.User", related_name="user", on_delete=models.CASCADE, db_column="user_id")
# Create your models here.
4 changes: 2 additions & 2 deletions pet_promotion/comment/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers

from .models import Comment
from board.models import Post
from board.models import Comment

class CommentSerializer(serializers.ModelSerializer):

Expand Down
16 changes: 12 additions & 4 deletions pet_promotion/comment/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework import viewsets

from .serializers import CommentSerializer
from .models import Comment
from board.models import Comment

@api_view(['POST'])
def comment_create(request, post_id):
serializer = CommentSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save(post=post_id)#post필드 값으로 post_id입력(어떤 게시물의 댓글인지 알기위함)
serializer.save(post_id=post_id)#post필드 값으로 post_id입력(어떤 게시물의 댓글인지 알기위함)
return Response(serializer.data)

@api_view(['PUT','DELETE']) #PUT : 데이터 수정할 때, DELETE : 데이터 삭제 할 때
Expand All @@ -21,4 +23,10 @@ def comment_update_and_delete(request, post_id, comment_id):
return Response({'message':'update'})
else:
comment.delete()
return Response({'message':'delete'})
return Response({'message':'delete'})

@api_view(['GET'])
def comment_list(request, post_id):
comment = Comment.objects.filter(post=post_id)
serializer = CommentSerializer(comment, many=True)
return Response(serializer.data)
1 change: 1 addition & 0 deletions pet_promotion/projects/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#App 등록
'user.apps.UserConfig',
'board.apps.BoardConfig',
'comment.apps.CommentConfig',

#DRF Settings
'rest_framework',
Expand Down
9 changes: 9 additions & 0 deletions pet_promotion/projects/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
from board.views import (
PostUpdateAPIView, PostDeleteAPIView, PostViewSet ,PostCreateViewSet,
)
from comment.views import (
comment_create, comment_update_and_delete, comment_list
)

router = routers.DefaultRouter()
router.register('post', PostViewSet, basename='post')
Expand All @@ -31,7 +34,13 @@
path('api/', include(router.urls)),
path('admin/', admin.site.urls),
path('user/', include('user.urls')),

url('api/post/(?P<id>[\w-]+)/edit/$', PostUpdateAPIView.as_view(), name='post_update'),
url('api/post/(?P<id>[\w-]+)/delete/$', PostDeleteAPIView.as_view(), name='post_delete'),

#comment
path('post/<int:post_id>/', comment_list),
path('post/<int:post_id>/comments/', comment_create),
path('post/<int:post_id>/comments/<int:comment_id>/',comment_update_and_delete),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)