[Answered ]-Django rest create user if login not exists

1👍

As per the condition you have said : You are creating a user object if the one provided does not exist (correct me if I’m wrong)

In that case the create method should be logically outside if condition where you are checking whether the user obj exists:

class UserSerializer(serializers.ModelSerializer):
    records = RecordSerializer(read_only=True, many=True)     
    def create(self, validated_data):
        if User.objects.filter(**validated_data).exists():
            raise Exception('User already exists')
        return User.objects.create(**validated_data)
👤Neeraj

Leave a comment