Django登录错误:save()得到了意外的关键字参数"update_fields",当我尝试登录时会发生这种情况 [英] Django login Error: save() got an unexpected keyword argument 'update_fields', this happens when i try to login

查看:53
本文介绍了Django登录错误:save()得到了意外的关键字参数"update_fields",当我尝试登录时会发生这种情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试登录时遇到一个错误.仅当我在用户模型中添加 save方法时,该错误才会发生.但是,当我删除save方法时,完全没有错误.我想使用 PIL 降低用户图片的质量,以更快地加载网站,这就是为什么我添加了save方法.谢谢!

I just got an error when I try to log in. the error happens only when I add the save method in the User model. But when I remove the save method there is no error at all. I want to decrease the quality of the user picture using PIL to load the website faster, that's why I add the save method. THANKS!

这是用户模型:

class User(AbstractUser):
    is_student = models.BooleanField(default=False)
    is_lecturer = models.BooleanField(default=False)
    is_parent = models.BooleanField(default=False)
    phone = models.CharField(max_length=60, blank=True, null=True)
    address = models.CharField(max_length=60, blank=True, null=True)
    picture = models.ImageField(upload_to='profile_pictures/%y/%m/%d/', default='default.png', null=True)
    email = models.EmailField(blank=True, null=True)

    username_validator = ASCIIUsernameValidator()

    def __str__(self):
         return '{} {}'.format(self.first_name, self.last_name, self.email)

    @property
    def get_full_name(self):
        full_name = self.username
        if self.first_name and self.last_name:
            full_name = self.first_name + " " + self.last_name
        return full_name

    def get_absolute_url(self):
        return reverse('profile_single', kwargs={'pk': self.pk})

    def save(self):
        super().save()
        try:
            img = Image.open(self.picture.path)
            if img.height > 300 or img.width > 300:
                output_size = (300, 300)
                img.thumbnail(output_size)
                img.save(self.picture.path)
        except:
            pass

    def delete(self, *args, **kwargs):
        if self.picture.url != settings.MEDIA_URL + 'default.png':
            self.picture.delete()
        super().delete(*args, **kwargs)

错误的完整日志在这里:

The full log of the error is here:

Internal Server Error: /accounts/login/
Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Python37\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Python37\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Python37\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Python37\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Python37\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "C:\Python37\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Python37\lib\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Python37\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Python37\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Python37\lib\site-packages\django\contrib\auth\views.py", line 61, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "C:\Python37\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Python37\lib\site-packages\django\views\generic\edit.py", line 142, in post
    return self.form_valid(form)
  File "C:\Python37\lib\site-packages\django\contrib\auth\views.py", line 90, in form_valid
    auth_login(self.request, form.get_user())
  File "C:\Python37\lib\site-packages\django\contrib\auth\__init__.py", line 132, in login
    user_logged_in.send(sender=user.__class__, request=request, user=user)
  File "C:\Python37\lib\site-packages\django\dispatch\dispatcher.py", line 175, in send
    for receiver in self._live_receivers(sender)
  File "C:\Python37\lib\site-packages\django\dispatch\dispatcher.py", line 175, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "C:\Python37\lib\site-packages\django\contrib\auth\models.py", line 20, in update_last_login
    user.save(update_fields=['last_login'])
TypeError: save() got an unexpected keyword argument 'update_fields'
[06/Sep/2020 15:25:19] "POST /accounts/login/?next=/ HTTP/1.1" 500 132787

推荐答案

由于声誉我无法发表评论,对不起.但我将在这里留下我的评论.

我认为,您提供的代码足以显​​示错误所在.

I don't think, the code you provided shows enough to see where the error is.

请提供此追溯的上下文:

Please provide context to this traceback:

  File "C:\Python37\lib\site-packages\django\contrib\auth\models.py", line 20, in update_last_login
    user.save(update_fields=['last_login'])

似乎还建议您,在可以让django替您完成此操作时,手动更新 last_login 字段.

Also suggestion, you seem to be, manually updating last_login field when you can make django do it for you.

    dtLastUpdate = models.DateTimeField(
        auto_now=True,
        editable=False,
    )

通过将 auto_now 设置为 True (仅适用于DateTimeField ).每次修改任何字段的对象时,它都会更新该字段.

By setting auto_now to True (for DateTimeField Only). It updates the field every time the object has been modified of any field.

无论如何,我认为最好将 super().save()放在 img.save(self.picture.path)之后,因为您在中进行了操作> try 子句与对象的ID(及其他ID)无关.

Anyway, I think its best to put super().save() after img.save(self.picture.path) since the operation in your try clause has nothing to do with the id of the object (and other such).

更早地执行 save()方法不会保存您在 try 子句中进行的操作.

Executing the save() method earlier won't save the operation you have in try clause.

这篇关于Django登录错误:save()得到了意外的关键字参数"update_fields",当我尝试登录时会发生这种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆