【问题标题】:Cannot force an update in save() with no primary key无法在没有主键的情况下强制更新 save()
【发布时间】:2021-01-04 19:03:15
【问题描述】:

我正在使用 Django 中的自定义构建用户模型,在stackoverflow link 中给出的migrate 时,终端中有一些消息。

但是,我没能解决这个问题,它并没有妨碍项目的运行,所以我实际上忽略了它并继续我的代码。现在,当我尝试登录 admin panel(http://127.0.0.1:8000/admin/) 时,它显示以下错误:

Internal Server Error: /admin/login/
Traceback (most recent call last):
  File "G:\Python\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "G:\Python\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "G:\Python\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "G:\Python\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "G:\Python\lib\site-packages\django\contrib\admin\sites.py", line 407, in login
    return LoginView.as_view(**defaults)(request)
  File "G:\Python\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "G:\Python\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "G:\Python\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "G:\Python\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "G:\Python\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "G:\Python\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "G:\Python\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "G:\Python\lib\site-packages\django\contrib\auth\views.py", line 63, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "G:\Python\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "G:\Python\lib\site-packages\django\views\generic\edit.py", line 142, in post
    return self.form_valid(form)
  File "G:\Python\lib\site-packages\django\contrib\auth\views.py", line 92, in form_valid
    auth_login(self.request, form.get_user())
  File "G:\Python\lib\site-packages\django\contrib\auth\__init__.py", line 131, in login
    user_logged_in.send(sender=user.__class__, request=request, user=user)
  File "G:\Python\lib\site-packages\django\dispatch\dispatcher.py", line 173, in send
    return [
  File "G:\Python\lib\site-packages\django\dispatch\dispatcher.py", line 174, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "G:\Python\lib\site-packages\django\contrib\auth\models.py", line 20, in update_last_login
    user.save(update_fields=['last_login'])
  File "G:\Python\lib\site-packages\django\contrib\auth\base_user.py", line 66, in save
    super().save(*args, **kwargs)
  File "G:\Python\lib\site-packages\django\db\models\base.py", line 745, in save
    self.save_base(using=using, force_insert=force_insert,
  File "G:\Python\lib\site-packages\django\db\models\base.py", line 782, in save_base
    updated = self._save_table(
  File "G:\Python\lib\site-packages\django\db\models\base.py", line 847, in _save_table
    raise ValueError("Cannot force an update in save() with no primary key.")
ValueError: Cannot force an update in save() with no primary key.

【问题讨论】:

    标签: python django


    【解决方案1】:

    我遇到了同样的问题,我到处搜索。 我在下面附上的链接中找到了答案。

    1-更改数据库名称。 2-makemigrations 和迁移。

    但在我阅读 towen 的评论之前它没有工作!我一直在创作 之前的超级用户 迁移,这是错误的!

    您已在创建超级用户之前进行了迁移。 所以

    python manage.py makemigrations 
    

    然后

    python manage.py migrate
    

    那么所有的表都可以正常工作了,现在我们可以创建超级用户了,它会有主键并且可以工作

    python manage.py createsuperuser 
    

    https://github.com/nesdis/djongo/issues/3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-09
      • 2019-08-02
      • 2021-08-23
      • 2023-01-31
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      相关资源
      最近更新 更多