๐Ÿ“’ [ TIL ] 2022.06.20_43์ผ์ฐจ # Django Rest Framework (4)

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

[ 2022-06-20 (์›”) ์˜ค๋Š˜์˜ TIL ]

[ Today Learn ]

  • ์—ญ์ฐธ์กฐ ๋ณต์Šต
  • user admin ์ƒ์„ฑ
  • admin ์‹ฌํ™”
  • permission class ์‹ฌํ™”
  • gte, lte ๋“ฑ ๋‹ค์–‘ํ•œ field lookup ํ™œ์šฉ ๋ฐ ์ •๋ ฌ

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

  • ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ ๋ฌธ๋ฒ•
    1) contains : ํŠน์ • string์ด ํฌํ•จ๋œ object ์ฐพ๊ธฐ

    fullname์— "์ด๋ฆ„"์ด๋ผ๋Š” string์ด ํฌํ•จ๋œ ์‚ฌ์šฉ์ž๋“ค์„ ์ฐพ๋Š”๋‹ค.
     UserModel.objects.filter(fullname__contains="์ด๋ฆ„")

    2) startswith / endsith : ํŠน์ • string์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š”/๋๋‚˜๋Š” object ์ฐพ๊ธฐ

    email์ด "@naver.com"์œผ๋กœ ๋๋‚˜๋Š” ์‚ฌ์šฉ์ž๋“ค์„ ์ฐพ๋Š”๋‹ค.
     UserModel.objects.filter(email__endswith="@naver.com")

    3) gt / lt / gte / lte : ํŠน์ • ๊ฐ’๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜/์ž‘๊ฑฐ๋‚˜/ํฌ๊ฑฐ๋‚˜๊ฐ™๊ฑฐ๋‚˜/์ž‘๊ฑฐ๋‚˜๊ฐ™์€ object ์ฐพ๊ธฐ

    ์‚ฌ์šฉ์ž ํ”„๋กœํ•„์˜ ๋‚˜์ด๊ฐ€ 19 ์ด์ƒ์ธ ์‚ฌ์šฉ์ž๋“ค์„ ์ฐพ๋Š”๋‹ค.
     UserProfileModel.objects.filter(age__gte=19)

    4) in : ํŠน์ • list์— ํฌํ•จ๋œ object ์ฐพ๊ธฐ

    ์‚ฌ์šฉ์ž ํ”„๋กœํ•„์˜ ์šด๋™ ํ˜น์€ ๋…์„œ๋ฅผ ์ทจ๋ฏธ๋กœ ๊ฐ€์ง„ ์‚ฌ์šฉ์ž๋“ค์„ ์ฐพ๋Š”๋‹ค.
     UserProfileModel.objects.filter(hobby__name__in=["์šด๋™", "๋…์„œ"])
  • ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฌผ ์ •๋ ฌํ•˜๊ธฐ
    order_by๋ฅผ ์‚ฌ์šฉํ•ด queryset์„ ์ •๋ ฌ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

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

User.objects.all().order_by("join_date") # ๊ฐ€์ž…์ผ ์ˆœ ์ •๋ ฌ
User.objects.all().order_by("-join_date") # ๊ฐ€์ž…์ผ ์—ญ์ˆœ ์ •๋ ฌ
User.objects.all().order_by("?") # ๋žœ๋ค ์…”ํ”Œ
  • ์ฟผ๋ฆฌ์—์„œ and์™€ or ํ™œ์šฉ
    Q๋ฅผ ์‚ฌ์šฉํ•ด ์ฟผ๋ฆฌ์— and, or์„ ์ ์šฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

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

from django.db.models.query_utils import Q

class UserView(APIView)
    def get(self, request):
        # ์ทจ๋ฏธ ์ค‘ ์‚ฐ์ฑ…์ด ์žˆ๊ฑฐ๋‚˜ ๋‚˜์ด๊ฐ€ 19์‚ด๋ณด๋‹ค ๋งŽ๊ณ  ๊น€์”จ์ธ ์‚ฌ๋žŒ๋งŒ ํ•„ํ„ฐ ์‚ฌ๋žŒ๋งŒ ํ•„ํ„ฐ
        qyery = Q(hobby__name="์‚ฐ์ฑ…") | Q(age__gt=19, user__name__startswith="๊น€")

        # ์ทจ๋ฏธ ์ค‘ ์‚ฐ์ฑ…์ด ์žˆ์œผ๋ฉด์„œ ๋‚˜์ด๊ฐ€ 19์‚ด๋ณด๋‹ค ๋งŽ์€ ์‚ฌ๋žŒ๋งŒ ํ•„ํ„ฐ
        qyery = Q(hobby__name="์‚ฐ์ฑ…") & Q(age__gt=19)				
        user_profile_list = UserProfileModel.objects.filter(qyery)
  • serializer ์‹ฌํ™”
    serializer๋Š” ๋ฐ์ดํ„ฐ ์ง๋ ฌํ™” ์™ธ์—๋„ data validation, create, update ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

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

view์—์„œ partial/raise_exception๋ฅผ ์‚ฌ์šฉํ•ด ์˜ต์…˜์„ ํ™œ์šฉํ•˜๊ฑฐ๋‚˜ serializer์—์„œ ๊ฐ ํ•„๋“œ์— ํ•ด๋‹นํ•˜๋Š” ๋‹ค์–‘ํ•œ ์˜ต์…˜๋“ค์„ ์ง€์ •ํ•ด์ฃผ๋Š” ๋“ฑ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์˜ต์…˜๋“ค์„ ํ™œ์šฉํ•ด์„œ ์†์‰ฝ๊ฒŒ curd ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฒŒ ๊ฐˆ์ˆ˜๋ก ์ ์  ์ƒˆ๋กœ์›Œ์ง€๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋‹ค๋งŒ, ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ธฐ๋Šฅ๋“ค์„ ์ตํžˆ๋Š”๋ฐ์—๋Š” ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆด ๊ฒƒ ๊ฐ™๋‹ค๋Š”๊ฒŒ ๊ด€๊ฑด์ธ๋“ฏํ•˜๋‹ค. ๊ฐ•์˜๊ฐ€ ๋๋‚ ๋•Œ๋งˆ๋‹ค ์ฃผ์–ด์ง€๋Š” ๊ณผ์ œ๋ฅผ ์ง„ํ–‰ํ• ๋•Œ๋งˆ๋‹ค ๊ทธ๋ž˜๋„ ์ ์–ด๋„ ํ•œ๊ฐ€์ง€์”ฉ์ด๋ผ๋„ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์–ด์„œ ์ข‹๋‹ค.

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

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

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

์—ด์‹ฌํžˆ ๊ณต๋ถ€ํ•˜๋Š”๊ฒŒ ๋ณด๊ธฐ์ข‹๋„ค์š” ^^~ ํ™”์ดํŒ…ํ™”์ดํŒ…~
โ•ฐ(ยฐโ–ฝยฐ)โ•ฏ

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

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