Saturday, June 22, 2019

extend New user model

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d86398b5-cd28-4ba1-8301-5dec4514ab0e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment