๐Ÿ“’ [ TIL ] 2022.06.16_41์ผ์ฐจ # Django Rest Framework (2)

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

[ 2022-06-16 (๋ชฉ) ์˜ค๋Š˜์˜ TIL ]

[ Today Learn ]

  • views.py์—์„œ ๋ฆฌํ€˜์ŠคํŠธ ์ฒ˜๋ฆฌํ•˜๊ธฐ
  • queryset, object์˜ ์ฐจ์ด
  • custom user ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉ์ž ๋กœ๊ทธ์ธ ๊ตฌํ˜„
  • settings.py์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์„ค์ •

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

  • views.py์—์„œ์˜ ๋ฆฌํ€˜์ŠคํŠธ

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

from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import permissions

class UserView(APIView): # CBV ๋ฐฉ์‹
    permission_classes = [permissions.AllowAny] # ๋ˆ„๊ตฌ๋‚˜ view ์กฐํšŒ ๊ฐ€๋Šฅ
    # permission_classes = [permissions.IsAdminUser] # admin๋งŒ view ์กฐํšŒ ๊ฐ€๋Šฅ
    # permission_classes = [permissions.IsAuthenticated] # ๋กœ๊ทธ์ธ ๋œ ์‚ฌ์šฉ์ž๋งŒ view ์กฐํšŒ ๊ฐ€๋Šฅ

    def get(self, request):
        return Response({'message': 'get method!!'})
        
    def post(self, request):
        return Response({'message': 'post method!!'})

    def put(self, request):
        return Response({'message': 'put method!!'})

    def delete(self, request):
        return Response({'message': 'delete method!!'})
  • queryset, object์˜ ์ฐจ์ด

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

  * object : ํ…Œ์ด๋ธ”์— ์ž…๋ ฅ๋œ ํŠน์ • ๋ ˆ์ฝ”๋“œ
   * queryset : object์˜ ์ง‘ํ•ฉ ex) [object(1), object(2), object(3)]
   
   	Model.objects.get(id=obj_id) # => return object
Model.objects.filter(date=datetime.today()) # => return queryset
  • custom user ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉ์ž ๋กœ๊ทธ์ธ ๊ตฌํ˜„
    ์ผ๋ฐ˜ user model์€ ํ•„๋“œ๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ์–ด ์ปค์Šคํ…€์ด ์–ด๋ คdnj custom user model ์ƒ์„ฑ ์‹œ ํ•„๋“œ๋“ค์„ ์ž์œ ๋กญ๊ฒŒ ์ปค์Šคํ…€ ๊ฐ€๋Šฅํ•˜๋‹ค.

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

# models.py
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser

# custom user model ์‚ฌ์šฉ ์‹œ UserManager ํด๋ž˜์Šค์™€ create_user, create_superuser ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์–ด์•ผ ํ•จ
class UserManager(BaseUserManager):
    def create_user(self, username, password=None):
        if not username:
            raise ValueError('Users must have an username')
        user = self.model(
            username=username,
        )
        user.set_password(password)
        user.save(using=self._db)
        return user
    
    # python manage.py createsuperuser ์‚ฌ์šฉ ์‹œ ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉ๋จ
    def create_superuser(self, username, password=None):
        user = self.create_user(
            username=username,
            password=password
        )
        user.is_admin = True
        user.save(using=self._db)
        return user

class User(AbstractBaseUser):
    username = models.CharField("์‚ฌ์šฉ์ž ๊ณ„์ •", max_length=20, unique=True)
    email = models.EmailField("์ด๋ฉ”์ผ ์ฃผ์†Œ", max_length=100)
    password = models.CharField("๋น„๋ฐ€๋ฒˆํ˜ธ", max_length=128)
    fullname = models.CharField("์ด๋ฆ„", max_length=20)
    join_date = models.DateTimeField("๊ฐ€์ž…์ผ", auto_now_add=True)

		# is_active๊ฐ€ False์ผ ๊ฒฝ์šฐ ๊ณ„์ •์ด ๋น„ํ™œ์„ฑํ™”๋จ
    is_active = models.BooleanField(default=True) 

    # is_staff์—์„œ ํ•ด๋‹น ๊ฐ’ ์‚ฌ์šฉ
    is_admin = models.BooleanField(default=False)
    
    # id๋กœ ์‚ฌ์šฉ ํ•  ํ•„๋“œ ์ง€์ •.
    # ๋กœ๊ทธ์ธ ์‹œ USERNAME_FIELD์— ์„ค์ • ๋œ ํ•„๋“œ์™€ password๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค.
    USERNAME_FIELD = 'username'

    # user๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ž…๋ ฅ๋ฐ›์€ ํ•„๋“œ ์ง€์ •
    REQUIRED_FIELDS = []
    
    objects = UserManager() # custom user ์ƒ์„ฑ ์‹œ ํ•„์š”
    
    def __str__(self):
        return self.username

    # ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž์˜ ํŠน์ • ํ…Œ์ด๋ธ”์˜ crud ๊ถŒํ•œ์„ ์„ค์ •, perm table์˜ crud ๊ถŒํ•œ์ด ๋“ค์–ด๊ฐ„๋‹ค.
    # admin์ผ ๊ฒฝ์šฐ ํ•ญ์ƒ True, ๋น„ํ™œ์„ฑ ์‚ฌ์šฉ์ž(is_active=False)์˜ ๊ฒฝ์šฐ ํ•ญ์ƒ False
    def has_perm(self, perm, obj=None):
        return True
    
    # ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž์˜ ํŠน์ • app์— ์ ‘๊ทผ ๊ฐ€๋Šฅ ์—ฌ๋ถ€๋ฅผ ์„ค์ •, app_label์—๋Š” app ์ด๋ฆ„์ด ๋“ค์–ด๊ฐ„๋‹ค.
    # admin์ผ ๊ฒฝ์šฐ ํ•ญ์ƒ True, ๋น„ํ™œ์„ฑ ์‚ฌ์šฉ์ž(is_active=False)์˜ ๊ฒฝ์šฐ ํ•ญ์ƒ False
    def has_module_perms(self, app_label): 
        return True
    
    # admin ๊ถŒํ•œ ์„ค์ •
    @property
    def is_staff(self): 
        return self.is_admin
# views.py
from django.contrib.auth import login, authenticate

class UserApiView(APIView):
    # ๋กœ๊ทธ์ธ
    def post(self, request):
        username = request.data.get('username', '')
        password = request.data.get('password', '')

        user = authenticate(request, username=username, password=password)
        if not user:
            return Response({"error": "์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ณ„์ •์ด๊ฑฐ๋‚˜ ํŒจ์Šค์›Œ๋“œ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."}, status=status.HTTP_401_UNAUTHORIZED)

        login(request, user)
        return Response({"message": "๋กœ๊ทธ์ธ ์„ฑ๊ณต!!"}, status=status.HTTP_200_OK)
  • settings.py ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์„ค์ •
    (1) debug = True / False debug ๋ชจ๋“œ ์„ค์ •. static file ์ฒ˜๋ฆฌ, allow host, ์—๋Ÿฌ ํŽ˜์ด์ง€ ๋“ฑ์˜ ์„ค์ •์ด ๋‹ฌ๋ผ์ง„๋‹ค.
    (2) LANGUAGE_CODE = 'ko-kr' : ์–ธ์–ด ์„ค์ •
    (3) TIME_ZONE = 'Asia/Seoul' : Timezone ์„ค์ •
    (4) DATABASES : DB ์„ค์ •
    (5) INSTALLED_APPS : ์‚ฌ์šฉํ•  ์•ฑ ์„ค์ •

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

์ฒ˜์Œ ์ ‘ํ•˜๋Š” django์— ๋‚ด์žฅ๋œ ๋ชจ๋“ˆ์ด๋‚˜ ํ•จ์ˆ˜๋“ฑ์„ ์•Œ๊ฒŒ๋˜๋‹ˆ ๋จธ๋ฆฌ๊ฐ€ ์–ด์งˆํ–ˆ๋‹ค. ๋‹น์žฅ ๊ฐ•์˜๋ฅผ ๋“ฃ๋Š”๋‹ค๊ณ  ์•„๋Š”๊ฒŒ ์ค‘์š”ํ•œ๊ฒŒ ์•„๋‹ˆ๋ผ ๋‚ด๊ฐ€ ์‹ค์ œ๋กœ ์ฝ”๋“œ๋ฅผ ๊ฐœ๋ฐœํ•˜๋ฉด์„œ ์‚ฌ์šฉํ•˜๊ฒŒ ๋์„๋•Œ ์•„๋ฌด๋Ÿฐ ์–ด๋ ค์›€์—†์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋‚ด๋ ค๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€๊ฐ€ ์ค‘์š”ํ• ํ…๋ฐ ์ž์‹ ์ด ์—†์—ˆ๋‹ค. ๊ทธ ๋งŒํผ ๋‚ด๊ฐ€ ๋” ๋…ธ๋ ฅํ•ด์•ผ๋œ๋‹ค๋Š” ๊ฑฐ๊ฒ ์ง€๋งŒ, ์ผ๋‹จ ๋“ค์—ˆ๋˜ ๊ฐ•์˜๋ฅผ ๋ณต์Šตํ•˜๋ฉด์„œ ์นœํ•ด์ ธ์•ผ๊ฒ ๋‹ค.

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

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

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

๋ณต์Šต์„ ํ•˜์‹œ๋ฉด์„œ ์ž์‹ ๊ฐ์„ ์ฐพ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค ^^

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

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