๊ธฐ์กด์๋ ์ ์๋ user๋ง ์กด์ฌํ์๋ค.
ํ์ง๋ง ํ์๋ user์ ํฌํจ์์ผ์ผ ํ๋ ์ด์ ๊ฐ ์๊ฒผ๋ค. (for B2C and B2B2C)
from django.contrib.auth.models import User
๊ธฐ์กด์ ์ฌ์ฉํ๋ User์ด๋ค.

User class๋ ์ ์ฌ์ง์ฒ๋ผ ๊ตฌ์ฑ๋์ด ์๋๋ฐ,
is_staff๋ django admin๊ถํ (superuser๋ก ์์ฑํ๋ฉด True์ด๋ค)
is_active๋ ์ค๋ซ๋์ ์ฌ์ฉํ์ง ์์ ID๋ฅผ ๋นํ์ฑํ (์ง์๋๋ฉด ์ ๊ฑฐ)
is_superuser๋ ๋ชจ๋ ๊ถํ์ ๊ฐ์ง flag๋ฅผ ๋ปํ๋ค.
๐๋ฐ๋ผ์ student user๋ฅผ ๊ตฌ๋ถ์ง๊ธฐ ์ํ flag๋ ์กด์ฌํ์ง ์๋๋ค!!!
(์์ ์๋ flag๋ฅผ ์ฌ์ฉํ๋ฉด ์ผ๋ฐ user๊ฐ ๊ถํ์ ๋๋ฌด ๋ง์ด ๊ฐ๊ฒ ๋๋ค)
profile์ ๋ง๋ค์ด User์ is_student boolean flag๋ฅผ ๊ฐ์ง table์ด OneToOne๊ด๊ณ๋ฅผ ๊ฐ๊ฒํ๋ค.
์ข์ ๋ฐฉ๋ฒ์ด์ง๋ง, ๊ฒฐ๊ตญ ๋์ค์๋ student๊ฐ ์ฌ์ฉํ ์ ์๋ page์ teacher์ด ์ฌ์ฉํ ์ ์๋ page(url)๋ฅผ ๊ตฌ๋ถ์์ผ์ค์ผ ํ๊ธฐ ๋๋ฌธ์ ์๋ก์ด app์ ๋ง๋ค๊ธฐ๋ก ๊ฒฐ์ !!
python manage.py startapp teacher
User๋ teacher๋ง ๊ฐ์ง๊ณ ์์๋ค. ๊ธฐ์กด์ ์๋ database์ ํ๋์ฉ ์์์ ์ผ๋ก relation์ ๋ง๊ฒ๋ ์ถ๊ฐํด์ผํ๋ค.
๋์ฐํ๋ค. database ์ด๋์ ๋ ์ง์์ฃผ๊ณ ์์ํ์(๊ฒฐ๊ตญ ๋ค์ง์)
๐User๋ฅผ Root๋ก ํ๊ณ teacher ์ student ํ
์ด๋ธ๋ผ๋ฆฌ ๊ด๊ณ๋ฅผ ๊ฐ๊ฒ ํ์๋ค.
ROOT <-> teacher OneToOne
ROOT <-> student OneToOne
teacher -> student OneToMany (์ ์๋์ ๋ง์ ํ์์ ํ๋ฒ์ ๊ฐ๋ฅด์น๋ค๋ ๊ฐ์ )


์ 2๊ฐ์ง๋ฅผ ์ถ๊ฐํด์ฃผ๊ณ migrate ๊ณผ์
python manage.py makemigrations
python manage.py migrate
teacher์ ๊ธฐ์กด์ student url ๋ถ๋ถ์ copy and paste ํด์ค์ผ๋ก์จ teacher์ด ์ฌ์ฉํ ํ์ด์ง๋ ์์ฑ๋์๋ค.
๐คฃ์ค๊ฐ์ ์๊พธ error๊ฐ ๋์ database์์ user๋ค์ ๋ค ์ง์์คฌ๋ค.
์๋ง ๊ด๊ณ๊ฐ ์๋ก ๊ผฌ์ด๋ฉด์ ์๊ธด error์ธ ๊ฒ ๊ฐ๋ค.
Front ๋จ์ End point๋ /student์์ /teacher๋ก ๋ฐ๊ฟ์ฃผ์๋ค.
๐ฅฒVS code์์๋ Pycharm๊ณผ ctrl + s๋ก ์ ์ฒด ์ ์ฅ์ด ์๋๊ธฐ ๋๋ฌธ์, ์ ์ฒด์ ์ฅ์ธ ctrl + shift + s๋ฅผ ์ฌ์ฉํด์ผํ๋ค.. (๋ด๋น ์ค์)
์ฌ์ ํ Error๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ Pycharm Debugging ๋ชจ๋๋ฅผ ์ฌ์ฉํด ์ฐพ์๋ณด์๋ค.
user/views.py ์์ ํ์๊ฐ์ ํ ๋ teacher์์ relationship์ ๊ดํ ๋ฌธ์ ์๋ค. ๋ฐ๋ผ์, ํ์๊ฐ์ ํ ๋ ์๋์ผ๋ก teacher์๋ ์์ฑํ๊ฒ๋ ํด์ฃผ์๋ค.

serializer์ front์์ ๋ณด๋ด๋ request.data ( username, email, password )๋ฅผ ๋ฐ์์จ๋ค.seralizer.save()๋ฅผ ํตํด ์ง๋ ฌํ๋ ํ์ผ์ ์ ์ฅํ๋ฉด user.id๊ฐ ์๊ธฐ๋๋ฐ serializer.instance๋ฅผ ํตํด teacher.id = user.id ๊ฐ ๋๊ฒ๋ ํด์ฃผ๊ณ teacher.name ์ user.username์ด๊ฒ ํด์ฃผ์๋ค.
teacher๊ฐ ํ์์ ์์ฑํด์ผํ๋ค. ์ด๋ค request ์ ๋ณด๋ฅผ ๋ฐ์์์ผํ ๊น? id์ ํ์์ ์ด๋ฆ ๊ทธ๋ฆฌ๊ณ email์ ๋ฐ์์ค๊ธฐ๋ก ๊ฒฐ์ ํ๋ค. password๋ ๊ด๋ฆฌ์๊ฐ ์๋์ผ๋ก ์์ฑํ๊ณ ๊ด๋ฆฌํ๊ฒ๋ ํด์ค๊ฒ์ด๋ค.

ํ์ ์ถ๊ฐ ๋ฒํผ์ ๋๋ฅด๋ฉด ascii letter์์ ๋๋ค์ผ๋ก 8๊ธ์๋ฅผ ๊ณจ๋ผ password๋ฅผ ์์ฑํด์ค๋ค. ์์ฑ๋ ์์ด๋์ ๋น๋ฐ๋ฒํธ๋ ๊ด๋ฆฌ์๊ฐ ๋ณผ ์ ์๊ฒ๋ ๋ง๋ค์ด์ฃผ์๋ค.

์ฆ, ์ ์ฒด์ ์ธ DB๋ฅผ ๋ดค์๋ ์ด๋ ๊ฒ ๋ฐ๊ฟ์ง ๊ฒ์ ๋ณผ ์ ์๋ค.

USER๊ฐ ROOT๋ก ์กด์ฌํ๊ณ ํ์๊ณผ ์ ์๋์ ๊ด๊ณ๋ฅผ ๋ํ๋ธ๋ค.
login์ ํ์ ๋, ์ด ๊ณ์ ์ด ํ์ ๊ณ์ ์ธ์ง ์ ์๋ ๊ณ์ ์ธ์ง ๊ตฌ๋ถํ ์ ์์ด์ผ ํ๋ค. ๋ฐ๋ผ์, FE์์ JWT TOKEN์ ๊ฒ์ฆ ๋ฐ์ ๋ Student table์๋ง ์กด์ฌํ๋ column์ด ์๋ค๋ฉด student role์, ์๋ค๋ฉด teacher role์ returnํ๊ฒ๋ ํด์ฃผ์๋ค. FE์์๋ RETURN๋ role๊ฐ์ ๋ฐํ์ผ๋ก teacher, student ์ ์ฉ route๋ฅผ ๋๋ ์ฃผ์๋ค.

ํ์ ์ ์ฉ page์๋ ์ ์๋์ด ํ ๋นํ ๋ฌธ์ ๋ง ๋ณผ ์ ์์ผ๋ฉด ๋๋ค.

๋ฐ๋ผ์ ๋ฐ๊ณผ ๊ฐ์ด Student/models.py๋ฅผ ์์ ํด์ฃผ์๋ค.
student๋ Task table๊ณผ Many to Many ๊ด๊ณ๋ฅผ ๊ฐ๊ฒ๋ ํด ์ฃผ์๋ค.

์ด MtoM relationship์ ์ฌ์ฉํ์ฌ Student๋ ์์ ์๊ฒ ํ ๋น๋ ๋ฌธ์ ์ง๋ง ๋ณผ ์ ์์ด์ก๋ค.

FE

BE
@permission_classes((IsAuthenticated,))
@authentication_classes((JSONWebTokenAuthentication,))
class QuestionView(APIView):
permission_classes = [IsAuthenticated]
authentication_classes = [JSONWebTokenAuthentication]
def get(self, request, task_id, format=None):
"""
Return a list of all questions for the given task ID.
"""
try:
task = Task.objects.get(id=task_id)
questions = Question.objects.filter(task=task)
if not questions.exists():
return Response({"detail": "No questions found for this task."}, status=status.HTTP_404_NOT_FOUND)
serializer = QuestionSerializer(questions, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
except Task.DoesNotExist:
return Response({"detail": "Task not found."}, status=status.HTTP_404_NOT_FOUND)
except Exception as e:
return Response({"detail": str(e)}, status=status.HTTP_400_BAD_REQUEST)
@permission_classes((IsAuthenticated,))
@authentication_classes((JSONWebTokenAuthentication,))
class TaskDeleteView(APIView):
def delete(self, request, task_id, format=None):
try:
tasks = Task.objects.filter(id=task_id)
tasks.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
except Task.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
ํด๋น Taskid์ ์กด์ฌํ๋ question๋ค์ ๋ฝ์์ Response๋ฅผ ํตํด ๋ณด๋ด์ค๋ค. ( TaskDeleteView๋ ํ์๋ ๋ฌธ์ ์ง์ ์ญ์ ํ ์ ์๋๋ก ๋ง๋ค์ด ์ฃผ์๋ค. ์ถํ์ ์ด ๊ธฐ๋ฅ์ด ํ์ํ์ง๋ ์๊ฐํด ๋ณผ ๋ฌธ์ ์ด๋ค.)

์์ ๊ฐ์ด ํด๋น Taskid์ ์ฐ๊ฒฐ๋ ๋ฌธ์ ๋ค์ ๋ณผ ์ ์๋ค.

์ด์ ํ์๋ค์ ์ ์๋์ ํ์ด์ง์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๋ค.