How can I search my entire Users model for users that already have the requested email/phone?

I am new to rest framework, so forgive me if this is an obvious answer. I am using serializers with querysets in my views.

The whole thing is locked down with JWT auth from Firebase

What is the best way to search my database for a user that has a certain email or phone and return a JSON response of True or False depending on if that email / phone already exists in the database?

What I have tried so far...

class getuserid(viewsets.ModelViewSet):
    queryset = UserProfile.objects.all()
    serializer_class = GetUserIdSerializer
    lookup_field = 'phone'

class GetUserIdSerializer(serializers.ModelSerializer):    
    class Meta:
        model=UserProfile
        fields=['user']

The problem with this method is if I use a JWT that is not associated with the user who has that phone number in their database model, I get a invalid token error/forbidden.

Obviously this is not a very secure way to do it, anyways, even if rest framework allowed me to.

Here is the serializer where I create my user's profile in my database...

class UserProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model=UserProfile
        fields=['company','email','name','phone','image','account_type','password','sub_user_of','sub_pending','products_read','products_write','restock_read','restock_write','expenses_read','expenses_write','banking_read','banking_write','admins']

class UserSerializer(serializers.ModelSerializer):    
    userprofile = UserProfileSerializer()

    def update(self, instance,validated_data):
        if(not instance.username == self.context['request'].user.username):
            raise exceptions.PermissionDenied('You do not have permission to update')
        profile_data = validated_data.pop('userprofile')
        if(not hasattr(instance,'userprofile')):
            instance.userprofile = UserProfile.objects.create(user=instance,**profile_data)
        else:
            instance.userprofile.company = profile_data["company"]
            instance.userprofile.email = profile_data["email"]
            instance.userprofile.name = profile_data["name"]
            instance.userprofile.phone = profile_data["phone"]
            instance.userprofile.account_type = profile_data["account_type"]
            instance.userprofile.password = profile_data["password"]
            instance.userprofile.save()
        instance.first_name = validated_data.get('first_name',instance.first_name)
        instance.last_name = validated_data.get('last_name',instance.last_name)
        instance.email = validated_data.get('email',instance.email)
        instance.save()
        return instance
    class Meta:
        model = User
        fields = ['last_name','first_name','userprofile']

What can I add to this that will do a lookup on my entire UserProfile model for users that already have the provided email and phone?

This was a lot simpler than I thought it would be...

I did not think it would let me query the entire database, not sure why I thought that, :)

Simply add this to the serializer...

if not profile_data["email"] == instance.userprofile.email:
            email_check = UserProfile.objects.filter(email=profile_data["email"])
            if email_check:
                raise exceptions.PermissionDenied('Email exists')
        if not profile_data["phone"] == instance.userprofile.phone:
            phone_check = UserProfile.objects.filter(phone=profile_data["phone"])
            if phone_check:
                raise exceptions.PermissionDenied('Phone exists')
Back to Top