๐Ÿ“’ [ TIL ] 2022.06.17_42์ผ์ฐจ # Django Rest Framework (3)

๋ฌธ๋ช…์ฃผยท2022๋…„ 6์›” 19์ผ
0
post-thumbnail

[ 2022-06-17 (๊ธˆ) ์˜ค๋Š˜์˜ TIL ]

[ Today Learn ]

  • ์—ญ์ฐธ์กฐ์— ๋Œ€ํ•œ ์ดํ•ด
  • serializer ํ™œ์šฉ
  • permission_classes๋ฅผ ํ™œ์šฉํ•œ ์ ‘๊ทผ ๊ถŒํ•œ ์„ค์ •

โœ๏ธ ๋‚ด๊ฐ€ ๋ฐฐ์šด๊ฒƒ, ์–ป์€๊ฒƒ

  • ์—ญ์ฐธ์กฐ

โ“ ์—ญ์ฐธ์กฐ๋ž€ ?

๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ€ ForeignKey๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ฑฐ๋‚˜ N:N ๊ด€๊ณ„์ธ ์ƒํ™ฉ, ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋ ค๋Š” ๊ฒฝ์šฐ

1) ์™ธ๋ž˜ ํ‚ค ์ง€์ • ์‹œ related_name ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด ์—ญ์ฐธ์กฐ ์‹œ ์‚ฌ์šฉ๋  ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
2) releated_name์„ ์ง€์ •ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ tablename_set ํ˜•ํƒœ๋กœ ์ง€์ •๋œ๋‹ค.

๐Ÿงฉ ์ ์šฉ ์˜ˆ์‹œ

- ex1) `user_profile.hobby` โ†’ ์ •์ฐธ์กฐ
- ex2) `hobby.userprofile_set` โ†’ hobby๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” UserProfile ํ…Œ์ด๋ธ”์˜ object๋ฅผ ์ ธ์˜ด
- models.py์—์„œ releated_name์„ `user_hobby` ๋กœ ์ง€์ •ํ–ˆ๋‹ค๋ฉด `hobby.user_hobby`์™€ ๊ฐ™์ด ์‚ฌ์šฉ

โ“ serializer๋ž€ ?

django์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ชจ๋ธ ์ธ์Šคํ„ด์Šค๋ฅผ REST API์—์„œ ์‚ฌ์šฉํ•˜๋Š” JSON์˜ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ์œผ๋กœ create, update ์‹œ validation ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

๐Ÿงฉ ์ ์šฉ ์˜ˆ์‹œ

serializer.py

from rest_framework import serializers

class UserSerializer(serializers.ModelSerializer):
   class Meta:
        # serializer์— ์‚ฌ์šฉ๋  model, field์ง€์ •
        model = User
        # ๋ชจ๋“  ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ fields = "__all__"๋กœ ์‚ฌ์šฉ
        fields = ["username", "password", "fullname", "email"]

views.py

from rest_framework.response import Response
from rest_framework import status

from user.serializers import UserSerializer

def get(self, request):
    user = request.user
    # serializer์— queryset์„ ์ธ์ž๋กœ ์ค„ ๊ฒฝ์šฐ many=True ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
    serialized_user_data = UserSerializer(user).data
    return Response(serialized_user_data, status=status.HTTP_200_OK)

# return data
"""
{
    "username": "user",
    "password": "pbkdf2_sha256$320000$u5YnmKo9luab9csqWpzRsa$pKfqHnBiF5Rgdo1Mj9nxNOdhpAl9AhPVXFPXkbPz7Mg=",
    "fullname": "user's name",
    "email": "user@email.com"
}
"""
  • permission_classes๋ฅผ ํ™œ์šฉํ•œ ์ ‘๊ทผ ๊ถŒํ•œ ์„ค์ •
    - permissions.AllowAny : ๋ชจ๋“  ์‚ฌ์šฉ์ž๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ ‘๊ทผ ํ—ˆ์šฉ
    - permissions.IsAuthenticated : ๋กœ๊ทธ์ธ ๋œ ์‚ฌ์šฉ์ž๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ ‘๊ทผ ํ—ˆ์šฉ
    - permissions.AllowAny : ๋ชจ๋“  ์‚ฌ์šฉ์ž๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ ‘๊ทผ ํ—ˆ์šฉ

๐ŸŒฑ ๋Š๋‚€ ์ 

serializer๋ฅผ ๋ฐฐ์šฐ๊ณ  ๋‚˜์„œ ์ž˜๋งŒ ์ด์šฉํ•  ์ค„ ์•Œ๋ฉด ์ •๋ง ์‰ฝ๊ณ  ๊ฐ„ํŽธํ•˜๊ฒŒ ์›ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ์‹ ๊ธฐํ–ˆ๊ณ , ์ด์— ์ต์ˆ™ํ•ด์ง€๋ ค๋ฉด ๋” ๋งŽ์ด ์ฝ”๋“œ๋ฅผ ์ณ๋ณด๊ณ  ๋” ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ๋Š๊ผˆ๋‹ค. ๋‚ด๊ฐ€ ์ž‘์„ฑํ•˜๋Š” ์ฝ”๋“œ๋Š” ์งง์•„์ ธ๋„ ์ด๋ฅผ ์ดํ•ดํ•˜๋Š”๋ฐ์—๋Š” ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ ค์„œ ๊ฑฑ์ •๋˜์ง€๋งŒ, ํ•˜๋‚˜ํ•˜๋‚˜ ๊พธ์ค€ํžˆ ๊ณ„์† ๊ณต๋ถ€ํ•˜๋‹ค๋ณด๋ฉด ์ดํ•ด๊ฐ€ ์ˆ˜์›”ํ•ด์ง€๊ฒ ์ง€!

profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ ๊พธ์ค€ํžˆ ๋‚˜์•„๊ฐ€๋Š” ๊ฐœ๋ฐœ์ž๐Ÿ™†โ€โ™€๏ธ https://github.com/Moonmooj

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2022๋…„ 6์›” 20์ผ

TIL์ด ์ ์  ์งง์•„์ง€์‹œ๋Š” ๊ฒƒ ๊ฐ™์•„์š”~

1๊ฐœ์˜ ๋‹ต๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด