Once try this may be usefull for you & **(my english is not good )just look 
at code
I am also getting this error using with "unique ", field i solved this my 
own way.
write serializers like this and dont write try,a dn except in serializers , 
define inside the function ,
 my user serializer ,
class UserSerializer(serializers.ModelSerializer):
# mobile = serializers.RegexField("[0-9]{10}",min_length=10,max_length=10)
# doctor_clinic=ClinicSerializer(read_only=True,many=True)
password = serializers.CharField(write_only=True)
email=serializers.EmailField(max_length=155,min_length=3,required=True)
name=serializers.CharField(max_length=55,min_length=3,required=True)

class Meta:
model = DoctorProfile
fields = ("id","name", "email", "password", "mobile","otp","dob","gender")
# fields="__all__"



def create(self, validated_data):
user = super(UserSerializer, self).create(validated_data)
user.set_password(validated_data['password'])
user.save()
return user
and 
my views

class RegisterApi(generics.GenericAPIView):
serializer_class = UserSerializer
authentication_classes = []

def post(self, request, *args, **kwargs):
parameters = request.data.copy()
parameters['otp'] = random.randrange(1000, 10000)
serializer = self.get_serializer(data=parameters)
if serializer.is_valid(raise_exception=True):
serializer.save()

return Response({"message": "User Created Successfully. Now perform Login 
to get your token"},status=status.HTTP_201_CREATED)
else:
return Response({'Mobile number already exist'
},status=status.HTTP_406_NOT_ACCEPTABLE)


On Friday, January 8, 2021 at 7:19:01 PM UTC+5:30 Barış Sermet wrote:

> Hi there!
>
> I have User model and serializer class which represents it.
>
> *class User(models.Model):*
> *    username = models.CharField(unique=True, max_length=50)*
> *    is_admin = models.BooleanField(default=False)*
>
> *class CreateUserSerializer(serializers.ModelSerializer):*
> *     class Meta:*
> *        model = User*
> *        fields = ("username", "is_admin")*
>
> *    def create(self, validated_data: Dict[str, Any]) -> User:*
> *        username = validated_data["username"]*
>
> *        try:*
> *            user_object = User.objects.create(username=username)*
> *        except IntegrityError:*
> *            raise UserAlreadyRegistered()*
>
> *        return user_object*
>
> My database holds lots of users(appr. 10 million). When I create another 
> user, Django Rest Framework look at the model fields and check that if 
> there are any unique=True fields.
> If there are, then it assigns "UniqueValidator". This validator calls 
> "filter_queryset" which means it query the database and check that 
> are there any given username record. If there are not, then it creates the 
> user.
>
> My question is, why DRF make a query before create operation? Since 
> database(PostgreSQL) holds the field attributes, it knows that 
> username field is unique, and it can throw an exception if I try to create 
> user with the same username field. The reason I'm asking it, my application 
> gets a lot of create operation, and each create operation I have to query 
> my database, which is I think more costly than create it wihout making a 
> filter query. 
> Do I missing something in this operation?
>
> Thanks!
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/8aa1b0cf-769c-4534-8a98-1cb2eb207f82n%40googlegroups.com.

Reply via email to