I have created my custom user model with the following code.

class UserManager(BaseUserManager):
    def create_user(self, email, name, phone, password=None):
        if not email:
            raise ValueError("Users must have an email address")

        email = self.normalize_email(email)
        email = email.lower()

        user = self.model(email=email, name=name, phone=phone)

        user.set_password(password)
        user.save(using=self._db)

        return user

    def create_vendor(self, email, name, phone, password=None):
        user = self.create_user(email, name, phone, password)

        user.is_vendor = True
        user.save(using=self._db)

        return user

    def create_superuser(self, email, name, phone, password=None):
        user = self.create_user(email, name, phone, password)

        user.is_superuser = True
        user.is_staff = True

        user.save(using=self._db)

        return user


class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=255, unique=True, db_index=True)
    name = models.CharField(max_length=255)
    phone = models.CharField(max_length=15, unique=True)
    otp = models.CharField(
        max_length=6,
    )
is_vendor = models.BooleanField(default=False)
    is_verified = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = ["name", "phone"]

    objects = UserManager()

    def __str__(self):
        return self.email

I have also created my view and i don't know where the issue is in the following code also:

class RegisterAPIView(views.APIView):
    permission_classes = (permissions.AllowAny,)

    def post(self, request):
        try:
            data = request.data
            print(data)
            email = data["email"].lower()

            name = data["email"]
            phone = data["phone"]
            password = data["password"]
            re_password = data["re_password"]
            is_vendor = data["vendor"]

            if is_vendor == "True":
                is_vendor = True
            else:
                is_vendor = False

            if re_password == password:
                if len(password) >= 8:
                    if not User.objects.filter(email=email).exists():
                        if not is_vendor:
                            User.objects.create_user(
                                name=name, email=email, phone=phone, password=password
                            )
                            return Response(
                                {
                                    "status": status.HTTP_201_CREATED,
                                    "message": "User created successfully",
                                }
                            )

                        else:
                            User.objects.create_vendor(
                                name=name, email=email, phone=phone, password=password
                            )
                            return Response(
                                {
                                    "status": status.HTTP_201_CREATED,
                                    "message": "Vendor Account created successfully",
                                }
                            )
                    else:
                        return Response(
                            {
                                "status": status.HTTP_400_BAD_REQUEST,
                                "message": "User with the given email already exists",
                            }
                        )
                else:
                    return Response(
                        {
                            "status": status.HTTP_400_BAD_REQUEST,
                            "message": "Passowrd must be atleast 8 characters",
                        }
                    )
            else:
                return Response(
                    {
                        "status": status.HTTP_400_BAD_REQUEST,
                        "message": "Passowrd and confirm password do not match",
                    }
                )

        except:
            return Response(
                {
                    "status": status.HTTP_500_INTERNAL_SERVER_ERROR,
                    "message": "Something went wrong",
                }
            )

I still get error when "Something went wrong when creating user" when i try to create a user or vendor using Postman. Please can someone guide me on what to do?

I have tried to print the data I am getting and it seems correct as the expected data

[04/Nov/2023 14:40:49] "POST /auth/register/ HTTP/1.1" 200 47
{'email': '', 'name': '', 'phone': '', 'password': 'efa9779ask@#', 're_password': 'efa9779ask@#', 'is_vendor': 'True'}

I was expecting a user created successfully, but getting "something went wrong"

0

There are 0 answers