About Django Shell and a Restored Database
When you are introspecting data in Django shell, and then restore the database in a parallel window, the database connection in Django shell gets broken and the next querysets will raise operational error:
>>> from django.contrib.auth.models import User
>>> User.objects.count()
1
>>> # at this point the database was restored...
>>> User.objects.count()
...
django.db.utils.OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
If you don't want to restart Django shell, just close the database connection and continue with your introspection:
>>> from django.db import connection
>>> connection.close()
>>> User.objects.count()
1
Tips and Tricks Programming Databases Developer Experience Django 6.x Django 5.2 Django 4.2 Python 3 PostgreSQL
Also by me
Django Messaging
For Django-based social platforms.
Django Paddle Subscriptions
For Django-based SaaS projects.
Django GDPR Cookie Consent
For Django websites that use cookies.