So i have tried to create Custome Users for my project Provider and Seeker, also i have create a register view, however, whenever i want to add a user (provider) it tells me that the email already exists, despite there is no users in the database yet
I have tried to remove the UniqueValidator from the ProviderSerializer and implement the logic of finding duplicate emails in the view
I have tried to save the user to the database then check if there is a email duplicate
from django.shortcuts import render
from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view, permission_classes, authentication_classes
from rest_framework.permissions import AllowAny, IsAdminUser
from .models import Provider, Seeker
from .serializer import ProviderSerializer
from django.contrib.auth.models import Group
from rest_framework import serializers
@api_view(['POST'])
@permission_classes([AllowAny])
def Register(request):
if request.data.get('work_email'):
seri = ProviderSerializer(data=request.data)
if seri.is_valid():
try:
user = seri.save()
try:
group = Group.objects.get(name='Provider')
user.groups.add(group)
except Group.DoesNotExist:
return Response({"Message": "Exception at group assigning"})
except serializers.ValidationError as e:
return Response(e.errors, status=status.HTTP_400_BAD_REQUEST)
return Response({"Message": "User created successfully"})
else:
return Response(seri.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return Response({"Message": "No Work_email"}, status=status.HTTP_400_BAD_REQUEST)
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class GenericUser(AbstractUser):
username = models.CharField(max_length=150, blank=True,null = True)
email = models.EmailField(max_length=255,unique=True,db_index=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS= []
class Provider(GenericUser):
work_email = models.EmailField(max_length=255, unique=True, blank=False, null = True)
class Seeker(GenericUser):
ed_level = models.EmailField(max_length=255, blank=True)