首页 > 解决方案 > Cutom 用户模型 save() 方法中的 Django 错误

问题描述

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

我认为 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.

标签: djangodjango-rest-framework

解决方案


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

我添加了身份验证后端。


推荐阅读