0
class UserAccountManager(BaseUserManager):
    def create_user(self, email, first_name, last_name, profile_pic, state, password=None):
        """
            Creates and saves a User with the given email, firstname,
            lastname, profile picture, state of residence and password.
        """
        if not email:
            raise ValueError('Users must have an email address')
        email = self.normalize_email(email)
        user = self.model(
            email=email,
            first_name=first_name,
            last_name=last_name,
            profile_pic=profile_pic,
            state=state
        )
        print("About to set the password")
        user.set_password(password)
        print("Set the user password, About to save")
        user.save()
        print("Saved the user")

        return user

class User(AbstractUser):
    email = models.EmailField(max_length=255, unique=True)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    profile_pic = models.ImageField(upload_to='images/', default="images/avatar.jpg")
    state = models.CharField(max_length=100)
    date_joined = models.DateTimeField(auto_now_add=True)

    objects = UserAccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name', 'profile_pic', 'state']

    class Meta:
        ordering = ('date_joined',)

    def __str__(self):
        return self.email

user.save() 没有运行。我尝试使用打印功能对其进行调试,发现“已保存用户”从未打印到终端,并且在向 API 发送发布请求时出现 404 错误。我使用 djoser API 进行身份验证。Djoser API 返回

POST /auth/users/

HTTP 400 Bad Request
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    "Unable to create account."
]

任何帮助将不胜感激。

4

0 回答 0