简体   繁体   中英

How to register a user with Email instead of username in Django Rest Framework

I've read tons of stack overflow posts about how to register a user with email instead of username but none of them seem to work, so I'm resetting to my defaults. I tried this and this but I still get 'username is required' back from the API when I try to register user.

I think the problem might be that I couldn't find a post that's explicit to Django 3 and uses Django Rest Framework.

Below are my models, serializers and views.

# models
class User(AbstractUser): 
    ROLES = (
        ('d', 'Doctor'),
        ('s', 'Secretary'),
        ('p', 'Patient'),
    )
    role = models.CharField(
        max_length=1, choices=ROLES, blank=True, default='p', help_text='Role')

class Doctor(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    clinic = models.ManyToManyField(
        Clinic, related_name="doctors", blank=True)
    appointment_duration = models.IntegerField(default=20, blank=True)

# serializer
User = get_user_model()
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'password', 'role', 'first_name', 'last_name', 'email', 'phone')
        extra_kwargs = {'password': {'write_only': True, 'required': True}

# viewset
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = (AllowAny,)

So, how could I register with email instead of username?

The username field in the Django can be used to store any string with such restrictions:

150 characters or fewer. Letters, digits and @/./+/-/_ only.

So it is possible to use username as an email too without any restrictions. You can override the Auth mechanism of the DRF to validate email in the provided username and validate the password. Check this link for details.

Also, you can find several approaches to extend the Django User model. Details are in this discussion.

Set the username to the email every time you save to keep them in sync. In model User :

class User(AbstractUser): 
    def save(self, *args, **kwargs): 
        self.username = self.email
        return super().save(*args, **kwargs)
    [...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM