๐งฉ ์ ์ฉ ์์
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!!'})
๐งฉ ์ ์ฉ ์์
* 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
๐งฉ ์ ์ฉ ์์
# 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)
debug = True / False
debug ๋ชจ๋ ์ค์ . static file ์ฒ๋ฆฌ, allow host, ์๋ฌ ํ์ด์ง ๋ฑ์ ์ค์ ์ด ๋ฌ๋ผ์ง๋ค.LANGUAGE_CODE = 'ko-kr'
: ์ธ์ด ์ค์ TIME_ZONE = 'Asia/Seoul'
: Timezone ์ค์ DATABASES
: DB ์ค์ INSTALLED_APPS
: ์ฌ์ฉํ ์ฑ ์ค์ ์ฒ์ ์ ํ๋ django์ ๋ด์ฅ๋ ๋ชจ๋์ด๋ ํจ์๋ฑ์ ์๊ฒ๋๋ ๋จธ๋ฆฌ๊ฐ ์ด์งํ๋ค. ๋น์ฅ ๊ฐ์๋ฅผ ๋ฃ๋๋ค๊ณ ์๋๊ฒ ์ค์ํ๊ฒ ์๋๋ผ ๋ด๊ฐ ์ค์ ๋ก ์ฝ๋๋ฅผ ๊ฐ๋ฐํ๋ฉด์ ์ฌ์ฉํ๊ฒ ๋์๋ ์๋ฌด๋ฐ ์ด๋ ค์์์ด ์ฝ๋๋ฅผ ์์ฑํด๋ด๋ ค๊ฐ ์ ์๋์ง๊ฐ ์ค์ํ ํ ๋ฐ ์์ ์ด ์์๋ค. ๊ทธ ๋งํผ ๋ด๊ฐ ๋ ๋ ธ๋ ฅํด์ผ๋๋ค๋ ๊ฑฐ๊ฒ ์ง๋ง, ์ผ๋จ ๋ค์๋ ๊ฐ์๋ฅผ ๋ณต์ตํ๋ฉด์ ์นํด์ ธ์ผ๊ฒ ๋ค.
๋ณต์ต์ ํ์๋ฉด์ ์์ ๊ฐ์ ์ฐพ๊ธฐ ๋ฐ๋๋๋ค ^^