extend New user model

4 views
Skip to first unread message

laya Mahmoudi

unread,
Jun 23, 2019, 10:24:12 AM6/23/19
to Django users
Hi , I am new to django.
my project is about a system which some users suck as student and Professors can sign up and login in the system and there are some courses which are defined with a professor and students can take the course.
my model is as follow:
class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    is_professor = models.BooleanField(default=False)


class Student(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    username = models.PositiveIntegerField()
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=100)
    student_no = models.PositiveIntegerField()


and my serializer is:

from rest_framework.serializers import ModelSerializer

from accounts.models import Student

User = get_user_model()


class StudentCreateSerializer(ModelSerializer):
    class Meta:
        model = Student
        fields = (
            'username',
            'first_name',
            'last_name',

        )

    def save(self, **kwargs):
        if Student.objects.last():
            new_student = Student.objects.last()
            student_no = new_student.student_no
            student_no = student_no+1
            Student.student_no = student_no
        else:
            student_no = 98012346
            Student.student_no = student_no
        user = User.objects.create_user(username=student_no, password=Student.username)
        Student.save(user)
        return Student
and my view is:
class StudentSigUpView(CreateAPIView):
    serializer_class = StudentCreateSerializer
    queryset = Student.objects.all()
I ask you to help me .Thanks

Reply all
Reply to author
Forward
0 new messages