title: Django login - "Cannot force an update in save() with no primary key"
body: <p>I have implemented the standard Django login in the following manner:</p>
  <pre><code>from django.contrib.auth import authenticate, login, logout
  
  def login_request(request):
      username = password = &quot;&quot;
      if request.POST:
          username = request.POST[&quot;username&quot;].lower()
          password = request.POST[&quot;password&quot;]
          user = authenticate(username=username, password=password)
          if user is not None:
              if user.is_active:
                  login(request, user)
                  return redirect(&quot;../..&quot;)
          return render(request, &quot;login.html&quot;, context={&quot;error&quot;: &quot;TRUE&quot;})
      return render(request, &quot;login.html&quot;, context={&quot;error&quot;: &quot;FALSE&quot;})
  </code></pre>
  <p>For most users, this has worked fine. However, for some users it has been throwing the following error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/core/handlers/exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/core/handlers/base.py&quot;, line 179, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;/home/ubuntu/qq_api/core/views.py&quot;, line 110, in login_request
      login(request, user)
    File &quot;/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/contrib/auth/__init__.py&quot;, line 132, in login
      user_logged_in.send(sender=user.__class__, request=request, user=user)
    File &quot;/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/dispatch/dispatcher.py&quot;, line 179, in send
      for receiver in self._live_receivers(sender)
    File &quot;/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/dispatch/dispatcher.py&quot;, line 179, in &lt;listcomp&gt;
      for receiver in self._live_receivers(sender)
    File &quot;/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/contrib/auth/models.py&quot;, line 20, in update_last_login
      user.save(update_fields=['last_login'])
    File &quot;/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/contrib/auth/base_user.py&quot;, line 67, in save
      super().save(*args, **kwargs)
    File &quot;/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/db/models/base.py&quot;, line 751, in save
      force_update=force_update, update_fields=update_fields)
    File &quot;/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/db/models/base.py&quot;, line 789, in save_base
      force_update, using, update_fields,
    File &quot;/home/ubuntu/qq_api/venv/local/lib/python3.6/site-packages/django/db/models/base.py&quot;, line 852, in _save_table
      raise ValueError(&quot;Cannot force an update in save() with no primary key.&quot;)
  
  Exception Type: ValueError at /login/
  Exception Value: Cannot force an update in save() with no primary key.
  </code></pre>
  <p>This error is sourced back to the <code>user.save(update_fields=['last_login'])</code> in <code>django/contrib/auth/models.py</code>. 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.</p>