Django login - "Cannot force an update in save() with no primary key"

Viewed 435

I have implemented the standard Django login in the following manner:

from django.contrib.auth import authenticate, login, logout

def login_request(request):
    username = password = ""
    if request.POST:
        username = request.POST["username"].lower()
        password = request.POST["password"]
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect("../..")
        return render(request, "login.html", context={"error": "TRUE"})
    return render(request, "login.html", context={"error": "FALSE"})

For most users, this has worked fine. However, for some users it has been throwing the following error:

Traceback (most recent call last):
  File "/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/ubuntu/qq_api/core/views.py", line 110, in login_request
    login(request, user)
  File "/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 132, in login
    user_logged_in.send(sender=user.__class__, request=request, user=user)
  File "/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 179, in send
    for receiver in self._live_receivers(sender)
  File "/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 179, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/contrib/auth/models.py", line 20, in update_last_login
    user.save(update_fields=['last_login'])
  File "/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 67, in save
    super().save(*args, **kwargs)
  File "/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/db/models/base.py", line 751, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/db/models/base.py", line 789, in save_base
    force_update, using, update_fields,
  File "/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/db/models/base.py", line 852, in _save_table
    raise ValueError("Cannot force an update in save() with no primary key.")

Exception Type: ValueError at /login/
Exception Value: Cannot force an update in save() with no primary key.

This error is sourced back to the user.save(update_fields=['last_login']) in django/contrib/auth/models.py. I have been unable to determine why this error is occurring, and why it happens to some users and not others. I've looked closely, and been unable to find anything that differentiates between users who have had this problem and those who haven't.

1 Answers

Correction under login function:

Delete username=password=""

Related