Search code examples
pythondjangoauthenticationpermissionsdjoser

How to use Djoser with multiple extended users?


Hello I am new to django django rest framework and djoser I was just wondering. How do I use djoser with multiple extended users. Below is my model, serializers and views. I can't really find anything in the internet. And the Djoser documentation doesn't have anything on multiple users. Should I just use the build in token authentication of Django Rest Framework?

Model: I am trying to inherit user with the student model and teacher model (as seen below). I want djoser to use these two model to create the users.

from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
# Create your models here.

class Student(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='student')
    age = models.IntegerField()
    address = models.CharField(max_length=200)

    def __str__(self):
        return self.user.username

class Teacher(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='teacher')
    description = models.TextField()

    def __str__(self):
        return self.user.username

class Course(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()
    price = models.FloatField(default=25.00)

    def __str__(self):
        return self.name

Serializer:

My serializer is not finished yet I still have to override the create and update methods.

from rest_framework import serializers
from api.models import *
from django.contrib.auth.models import User

class StudentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Student
        fields = ('age', 'address')

class StudentUserSerializer(serializers.ModelSerializer):

    student = StudentSerializer()

    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'password', 'first_name', 'last_name', 'student')
        extra_kwargs = {'password': {'write_only': True, 'required': True}}

class TeacherSerializer(serializers.ModelSerializer):
    class Meta: 
        model = Teacher
        fields = ('description', )

class TeacherUserSerializer(serializers.ModelSerializer):

    teacher = TeacherSerializer()

    class Meta:
        model = User
        fields = ('id', 'username', 'email', 'password', 'first_name', 'last_name', 'teacher') 
        extra_kwargs = {'password': {'write_only': True, 'required': True}}

class CourseSerializer(serializers.ModelSerializer):

    class Meta:
        model = Course
        fields = '__all__'

Views:

As well as my views I still have to setup permissions for the views.

from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from api.serializers import *
from api.models import *
from django.contrib.auth.models import User
# Create your views here.

class CourseView(ModelViewSet):
    serializer_class = CourseSerializer
    queryset = Course.objects.all()

class StudentView(ModelViewSet):
    serializer_class = StudentUserSerializer
    queryset = User.objects.all()

class TeacherView(ModelViewSet):
    serializer_class = TeacherUserSerializer
    queryset = User.objects.all()

Solution

  • Override the ModelViewset's perform_create() methods of TeacherView and StudentView to also create a user object.