【问题标题】:Django error in Cutom user model save() method自定义用户模型保存()方法中的 Django 错误
【发布时间】:2020-05-16 10:18:48
【问题描述】:

我有一个自定义用户模型,它可以正常工作,后来我为用户图像添加了字段,然后我可以注册但无法登录。

我认为 save() 方法有错误。

我不接受注册页面中的用户图片,但是有模板供登录用户稍后添加

from django.db import models
from django.contrib.auth.models import AbstractUser
from PIL import Image

class User(AbstractUser):
    email = models.EmailField(verbose_name='email' ,max_length=223,unique=True)
    photo = models.ImageField(upload_to='prof_pic', blank=True,null=True)
    phone=models.CharField(null=True,max_length=11)
    REQUIRED_FIELDS = [ 'email','phone']



    def save(self,*args, **kwargs):
        super().save()
        if self.photo:
            pic = Image.open(self.photo.path)
            if pic.height > 300 or pic.width > 300:
                output_size = (300, 300)
                pic.thumbnail(output_size)
                pic.save(self.photo.path)
            super(User, self).save(*args, **kwargs)        

登录时出错

Please enter a correct username and password. Note that both fields may be case-sensitive.

【问题讨论】:

  • 回溯到底是什么?您是否得到了类似于“已经存在 pk 81 的行”之类的东西?
  • 没有错误,只是 Django 服务器不接受我的用户名和密码
  • @Sahil ,你看到上面的代码有什么错误吗?

标签: django django-rest-framework


【解决方案1】:

https://stackoverflow.com/a/8845337/10515390 检查此链接上的答案。

我已添加身份验证后端。

【讨论】:

    猜你喜欢
    • 2019-10-18
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 2013-07-12
    • 2017-07-13
    • 2023-03-10
    • 1970-01-01
    • 2020-12-06
    相关资源
    最近更新 更多