π± Github
https://github.com/nicesugi/joo_test
μ€ν°λμμ DRF λ―Έλ νμμ΄νμ νμλ€.
.. 2νλ ν΄μΌνλλ°, λμ ν νμ¬ μν©μμ μ΄ν΄κ° μκ°κΈΈλ λ€μκΈ°νλ₯Ό λ Έλ¦¬κ³ μμκ³ ,
곡λΆνλ€κ° Django λ°λλΌμ½λλ κ°μ΄ λΉκ΅ν΄μ μμ±ν΄λ μ’κ² λ€λ μκ°μ
μ₯κ³ μ DRF λμ μ½λλ₯Ό κ°μ 쑰건?μμ μμ±ν΄λ³΄μλΌλ μκ°μ μμ±νκ² λμλ€.
* status μ½λλ μμ±νμ§ μμ
* DRF serializer μ΄μ©ν μ½λ
Django [κ²μκΈ μ‘°ν, μμ±, μμ , μμ ] μ΄κ°λ¨ μ½λ
class PostView(APIView):
# κ²μκΈ μ‘°ν
def get(self, request):
# 1 μμ±μμ κ²μκΈλ§! μ‘°ν
user = request.user
print(user)
posts = Post.objects.filter(user=user).values('user', 'content')
return Response ({'post': posts})
class PostView(APIView):
# κ²μκΈ μ‘°ν
# 2 μ 체 κ²μκΈ μ‘°ν
posts = Post.objects.values('user', 'content')
return Response ({'post': posts})
values μ€μ
# values() > id, user, content μ λΆ
# values('user', 'content') > user, content λ΄κ° μνλ κ²λ§
# κ²μκΈ μμ±
def post(self, request):
user = request.user
content = request.data.get('content', '')
content, created = Post.objects.get_or_create(
content = content
)
if created:
content.user = user
content.save()
print(f'content.id: {content}')
return Response({"message":"κ²μκΈ μμ± μ±κ³΅"})
# κ²μκΈ μμ
def put(self, request, obj_id):
fix_content = Post.objects.get(id=obj_id)
fix_content.content = request.data.get('content')
fix_content.save()
# κ²μκΈ μμ
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/models.py
λ°μν