π± Github
https://github.com/nicesugi/joo_test
μ€ν°λμμ DRF λ―Έλ νμμ΄νμ νμλ€.
.. 2νλ ν΄μΌνλλ°, λμ ν νμ¬ μν©μμ μ΄ν΄κ° μκ°κΈΈλ λ€μκΈ°νλ₯Ό λ Έλ¦¬κ³ μμ.
곡λΆνλ€κ° Django λ°λλΌμ½λλ κ°μ΄ λΉκ΅ν΄μ μμ±ν΄λ μ’κ² λ€λ μκ°μ
μ₯κ³ μ DRF λμ μ½λλ₯Ό κ°μ 쑰건?μμ μμ±ν΄λ³΄μλΌλ μκ°μ μμ±νκ² λμλ€.
* status μ½λλ μμ±νμ§ μμ
* μλμ modelκ³Ό serializer μ½λλ 첨λΆλ¨
DRF serializer μ΄μ©νμ¬ [κ²μκΈ μ‘°ν, μμ±, μμ , μμ ] μ½λ μμ±νκΈ°
class PostView(APIView):
# κ²μκΈ μ‘°ν
def get(self, request):
# 1 μμ±μμ κ²μκΈλ§! μ‘°ν
print(request.data)
user = request.user
return Response(UserSerializer(user).data)
class PostView(APIView):
# κ²μκΈ μ‘°ν
def get(self, request):
# 2 μ 체 κ²μκΈ μ‘°ν
posts = Post.objects.all()
serialized_data = PostSerializer(posts, many=True).data
return Response(serialized_data)
class PostView(APIView):
def post(self, request):
# κ²μκΈ μμ±
request.data['user'] = request.user.id
post_serializer = PostSerializer(data=request.data)
print(post_serializer)
if post_serializer.is_valid():
post_serializer.save()
return Response(post_serializer.data)
return Response(post_serializer.errors)
class PostView(APIView):
def put(self, request, obj_id):
# κ²μκΈ μμ
content = Post.objects.get(id=obj_id)
content_serializer = PostSerializer(content, data=request.data, context={"request": request})
print(f'content_serializer: {(content_serializer)}')
if content_serializer.is_valid():
content_serializer.save()
return Response({"message": "κ²μκΈ μμ μ±κ³΅"})
return Response({"message": "κ²μκΈ μμ μ€ν¨"})
class PostView(APIView):
def delete(self, request, obj_id):
# κ²μκΈ μμ
user = request.user
content = Post.objects.get(id=obj_id)
if user:
content.delete()
return Response({"message": "μμ μ±κ³΅"})
return Response({"message": "μμ μ€ν¨"})
π½ Post/serializers.py
π½ User/serializers.py
π½ Post/models.py
π½ User/models.py
λ°μν