I'm using django and djoser for backend authentication and react on frontend side. I want to put a condition on email in serializer that if email already exists send them error msg that "email already taken" otherwise register.
serializer
class UserCreateSerializer(UserCreateSerializer):
class Meta(UserCreateSerializer.Meta):
model = User
fields = ("id","username","email","password")
def validate_email(self, email):
if User.objects.filter(email=email).exist():
msg = "email already taken"
raise serializers.ValidationError(msg)
return email
djoser settings in settings.py
DJOSER = {
...,
"SERIALIZERS" : {
"user_create":"accounts.serializers.UserCreateSerializer",
"user":"accounts.serializers.UserCreateSerializer",
"user_delete":"djoser.serializers.UserDeleteSerializer",
},
}
accounts/models.py
from django.db import models
from django.contrib import auth
# Create your models here.
class User(auth.models.User,auth.models.PermissionsMixin):
def __str__(self):
return "@{}".format(self.username)
Many many thanks in advance reading and answering this question..