Signin error: Exception Value: Cannot force an update in save() with no primary key

I am using django with mongo db to create a social media website, but I run into the following error while signing in:


Request Method: POST
Request URL:

Django Version: 3.2.16
Python Version: 3.7.2
Installed Applications:
Installed Middleware:

Traceback (most recent call last):
  File "/Users/sparshbohra/django-social/venv/lib/python3.7/site-packages/django/core/handlers/", line 47, in inner
    response = get_response(request)
  File "/Users/sparshbohra/django-social/venv/lib/python3.7/site-packages/django/core/handlers/", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/sparshbohra/django-social/django-social-media-website/core/", line 230, in signin
    login(request, user)
  File "/Users/sparshbohra/django-social/venv/lib/python3.7/site-packages/django/contrib/auth/", line 135, in login
    user_logged_in.send(sender=user.__class__, request=request, user=user)
  File "/Users/sparshbohra/django-social/venv/lib/python3.7/site-packages/django/dispatch/", line 182, in send
    for receiver in self._live_receivers(sender)
  File "/Users/sparshbohra/django-social/venv/lib/python3.7/site-packages/django/dispatch/", line 182, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/Users/sparshbohra/django-social/venv/lib/python3.7/site-packages/django/contrib/auth/", line 22, in update_last_login['last_login'])
  File "/Users/sparshbohra/django-social/venv/lib/python3.7/site-packages/django/contrib/auth/", line 67, in save
    super().save(*args, **kwargs)
  File "/Users/sparshbohra/django-social/venv/lib/python3.7/site-packages/django/db/models/", line 740, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/sparshbohra/django-social/venv/lib/python3.7/site-packages/django/db/models/", line 778, in save_base
    force_update, using, update_fields,
  File "/Users/sparshbohra/django-social/venv/lib/python3.7/site-packages/django/db/models/", line 841, in _save_table
    raise ValueError("Cannot force an update in save() with no primary key.")

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

My imports and signin function:

from django.shortcuts import render, redirect
from django.contrib.auth.models import User, auth
from django.contrib.auth import authenticate, logout, login
from django.contrib import messages
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from .models import Profile, Post, LikePost, FollowersCount
from itertools import chain
import random

def signin(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(username=username, password=password)

        if user is not None:
            login(request, user)
            return redirect('/')
  , 'Credentials Invalid')
            return redirect('signin')

        return render(request, 'signin.html')

My imports and user model:

from djongo import models
from django.contrib.auth import get_user_model
import uuid
from datetime import datetime

User = get_user_model()

    'default': {
        'ENGINE': 'djongo',
        'CLIENT': {
            'host': 'mongodb+srv://',
            'name': 'reunion',
            'authMechanism': 'SCRAM-SHA-1' # for Atlas cloud db

Please tell me why am I getting the error and how should it be fixed. I have already tried makemigrations and migrate.

I think the issue is with your authenticate() method's implementation, I have 2 suggestions; either / or would be the fix.

  1. Define your authenticate method to use the django's auth user's .check_password() method:

In any file really

User = get_user_model()

def authenticate(request, username=None, password=None, **kwargs):
    _, _ = request, kwargs
        if User.objects.get(username=username).check_password(password):
            return user
    except User.DoesNotExist:
    return None 
  1. Use and register Django's default token obtain route to handle authentication:


from django.urls import path
from rest_framework_simplejwt import views as jwt_views

urlpatterns = [
    path('token/obtain/', jwt_views.TokenObtainPairView.as_view(), name='obtain_token'),
    path('token/refresh/', jwt_views.TokenRefreshView.as_view(), name='refresh_token'),

Back to Top