I have a problem with my django server time, it is 2 hours late.
I tried to find a solution but whatever i found (as How to change my django server time, or Django documentation) is not what I think I need.
Right now my computer's time is 23:14:37
When i write in bash date +%H:%M:%S I will get:
(python_env) bash-3.2$ date +%H:%M:%S
23:17:03
So I don't think this is my console problem.
But when I am running my server here is what I get:
(python_env) bash-3.2$ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
April 04, 2020 - 21:18:47
Django version 2.2.4, using settings 'myVote365.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
The time is now 2 hours late.
I have red that it could be because of wrong timezone, but I have my code similar to How to change my django server time
LANGUAGE_CODE = 'pl'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
What's more it worked perfectly fine just before I change my os to Windows, and to OS X again
Edit:
I changed USE_TZ to False, ran and closed the server, and again to True.
And now although I still have a server 2 hour late. But from error:
Internal Server Error: /panel/
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/contrib/sessions/backends/base.py", line 189, in _get_session
return self._session_cache
AttributeError: 'SessionStore' object has no attribute '_session_cache'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: django_session
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/icookiez/Documents/praca/dla taty/myVote365/myVote365/audytor/views.py", line 26, in panel
if 'auditor' in request.session and request.session['auditor']['logged'] is True:
File "/usr/local/lib/python3.7/site-packages/django/contrib/sessions/backends/base.py", line 51, in __contains__
return key in self._session
File "/usr/local/lib/python3.7/site-packages/django/contrib/sessions/backends/base.py", line 194, in _get_session
self._session_cache = self.load()
File "/usr/local/lib/python3.7/site-packages/django/contrib/sessions/backends/db.py", line 43, in load
s = self._get_session_from_db()
File "/usr/local/lib/python3.7/site-packages/django/contrib/sessions/backends/db.py", line 34, in _get_session_from_db
expire_date__gt=timezone.now()
File "/usr/local/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 402, in get
num = len(clone)
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 256, in __len__
self._fetch_all()
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: django_session
I got a working site.
First step
I changed USE_TZ to False, ran and closed the server, and again to True. This allowed my side to load, but still had the wrong time.
Second step
I changed TIME_ZONE not to ‘PL’ as #jatrp5 suggested, but to 'Europe/Warsaw' as I read on this question. Using the zone name form Wikipedia. And now I have time I was wishing for.
To match your bash time you should change the time zone to TIME_ZONE = 'PL'. The universal time zone (UTC) is two hours behind the Polish.
Related
i try to add usernames to my video chatting app and this error occured. I try to fetch create_member but this error occured. I try to solve many times but failed. Kindly Check it.
Uncaught (in promise) SyntaxError: Unexpected token. This error occured in console and i can't get my video and audio tracks.
Internal Server Error: /create_member/
Traceback (most recent call last):
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line
89, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\base.py", line 477, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: videoApp_roommember.name
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\hp\Desktop\ChattingApp\VideoChatting\videoApp\views.py", line 76, in createMember
member, created = RoomMember.objects.get_or_create(
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\manager.py", line
85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 657, in get_or_create
return self.get(**kwargs), False
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 492, in get
num = len(clone)
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 302, in __len__
self._fetch_all()
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 1507, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 57, in __iter__
results = compiler.execute_sql(
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\compiler.py",
line 1361, in execute_sql
cursor.execute(sql, params)
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line
103, in execute
return super().execute(sql, params)
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line
67, in execute
return self._execute_with_wrappers(
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line
80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line
84, in _execute
with self.db.wrap_database_errors:
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line
89, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\hp\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\sqlite3\base.py", line 477, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: videoApp_roommember.name
[11/Oct/2022 07:47:37] "POST /create_member/ HTTP/1.1" 500 141222
So, here is my views.py file:
#csrf_exempt
def createMember(request):
data = json.loads(request.body)
member, created = RoomMember.objects.get_or_create(
name=data['name'],
uid=data['UID'],
room_name=data['room_name']
)
return JsonResponse({'name':data['name']}, safe=False)
here is my script.js file
let createMember = async () => {
let response = await fetch('/create_member/', {
method:'POST',
headers: {
'Content-Type':'application/json'
},
body:JSON.stringify({'name':NAME, 'room_name':CHANNEL, 'UID':UID})
})
let member = await response.json()
return member
}
And here is models.py file
class RoomMember(models.Model):
name = models.CharField(max_length=200)
uid = models.CharField(max_length=1000)
room_name = models.CharField(max_length=200)
insession = models.BooleanField(default=True)
def __str__(self):
return self.name
In django's docs under migrations it says:
Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema.
So the error says
django.db.utils.OperationalError: no such column: videoApp_roommember.name
Which means the changes you made, which is adding name to the model, has not been migrated to take effect on the database table so whenever you make a query or try to create a room member the error above will be thrown because of not finding the name column in the DB.
So you need to:
Run python manage.py makemigrations to create the migrations file for the changes you made.
Run python manage.py migrate to make the updates to the database
I have a website in production. I have an app, with a model. It contained a markdown_file attribute:
markdown_file=models.FileField(upload_to='/media/')
But since the number of file is limited, I decided to make it a markdown_filename attribute with a choices selection box:
markdown_filename=models.CharField(max_length=30,null=True,blank=True,choices=MENU_MARKDOWN_FILE_CHOICES)
Therefore, I modified this model and made migrations locally. I pushed the code in production and run:
python manage.py migrate
After I checked with showmigrations and sqlmigrate, that the modification I made were there. Then, I checked in the database, that the fields were correctly modified. But, I still get this error, when trying to access the website:
ERROR:django.request:Internal Server Error: /
Traceback (most recent call last):
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column showcase_subpage.markdown_file does not exist
LINE 1: ...bpage"."slug", "showcase_subpage"."html_content", "showcase_...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./showcase/views.py", line 10, in home
return render(request, 'showcase/home.html', context)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/template/defaulttags.py", line 166, in render
len_values = len(values)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/db/models/query.py", line 256, in __len__
self._fetch_all()
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/srv/data/web/vhosts/default/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column showcase_subpage.markdown_file does not exist
LINE 1: ...bpage"."slug", "showcase_subpage"."html_content", "showcase_...
^
The markdown_file field has been modified and renamed as markdown_filename. It has been correctly specified in the migrations. The modification is effective in the database. I tested locally (it works fine locally), to make sure, I don’t have anymore code calling markdown_file attribute.
I also tried clearing the migrations history and the database, to rebuilt it as new, but, the problem is still there. I can’t see why, this former attributes is still queried, though the migrations went OK, without any error.
Django version 1.11, sqlite3 version 3.11.
I'm using WAL mode and a long timeout:
from django.apps import AppConfig
from django.db.backends.signals import connection_created
class SQLite3Config(AppConfig):
name = 'sqlite3_config'
def ready(self):
connection_created.connect(configure_sqlite)
# noinspection PyUnusedLocal
def configure_sqlite(sender, connection, **_):
if connection.vendor == 'sqlite':
cursor = connection.cursor()
cursor.execute('PRAGMA journal_mode=WAL;')
cursor.execute('PRAGMA busy_timeout=5000;')
I want to retain sqlite3 and not move to mysql or postgres because the application is small and is installed by users on multiple servers.
I believe WAL should allow "concurrent" writes by serializing them. The "Database is locked" problem was observed when small bursts (half a dozen or so) were received together.
I can reproduce the problem in the shell with threads. The django model method simply sets a flag and saves the model:
def activate(self):
self.activate = True
self.save()
When I use threads I find it fails if I launch a few threads that attempt it at the same time. There is no wait so the timeout is not involved. The error occurs before the 5 second busy timeout has elapsed (in less than two seconds):
In [2]: [NGThread(notifier_group.id).start() for notifier_group in NotifierGroup.objects.all()[:2]]
Out[2]: [None, None]
In [3]: [NGThread(notifier_group.id).start() for notifier_group in NotifierGroup.objects.all()[:3]]
Out[3]: [None, None, None]
In [4]: [NGThread(notifier_group.id).start() for notifier_group in NotifierGroup.objects.all()[:4]]
Out[4]: [None, None, None, None]
In [5]: Exception in thread Thread-97:
Traceback (most recent call last):
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: database is locked
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/home/paul/wk/cliosoft/sosadmin/scratch.py", line 41, in run
toggle_active(notifier_group)
File "/home/paul/wk/cliosoft/sosadmin/scratch.py", line 30, in toggle_active
model.activate()
File "/home/paul/wk/cliosoft/sosadmin/notifications/models/notifier_group.py", line 67, in activate
self.save()
File "/home/paul/wk/cliosoft/sosadmin/notifications/models/notifier_group.py", line 33, in save
self.verify()
File "/home/paul/wk/cliosoft/sosadmin/notifications/models/notifier_group.py", line 46, in verify
self.create_notifier(base_spec, model_set, group_event_condition)
File "/home/paul/wk/cliosoft/sosadmin/notifications/models/notifier_group.py", line 57, in create_notifier
notifier.users = self.users.all()
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 534, in __set__
manager.set(value)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 1004, in set
self.add(*new_objs)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 931, in add
self._add_items(self.source_field_name, self.target_field_name, *objs)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 1100, in _add_is
for obj_id in new_ids
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/query.py", line 442, in bulk_create
ids = self._batched_insert(objs_without_pk, fields, batch_size)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/query.py", line 1083, in _batched_insert
self._insert(item, fields=fields, using=self.db)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/query.py", line 1060, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
cursor.execute(sql, params)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: database is locked
Exception in thread Thread-98:
Traceback (most recent call last):
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: database is locked
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/home/paul/wk/cliosoft/sosadmin/scratch.py", line 41, in run
toggle_active(notifier_group)
File "/home/paul/wk/cliosoft/sosadmin/scratch.py", line 28, in toggle_active
model.deactivate()
File "/home/paul/wk/cliosoft/sosadmin/notifications/models/notifier_group.py", line 72, in deactivate
self.save()
File "/home/paul/wk/cliosoft/sosadmin/notifications/models/notifier_group.py", line 33, in save
self.verify()
File "/home/paul/wk/cliosoft/sosadmin/notifications/models/notifier_group.py", line 46, in verify
self.create_notifier(base_spec, model_set, group_event_condition)
File "/home/paul/wk/cliosoft/sosadmin/notifications/models/notifier_group.py", line 57, in create_notifier
notifier.users = self.users.all()
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 534, in __set__
manager.set(value)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 1004, in set
self.add(*new_objs)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 931, in add
self._add_items(self.source_field_name, self.target_field_name, *objs)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 1100, in _add_is
for obj_id in new_ids
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/query.py", line 442, in bulk_create
ids = self._batched_insert(objs_without_pk, fields, batch_size)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/query.py", line 1083, in _batched_insert
self._insert(item, fields=fields, using=self.db)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/query.py", line 1060, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
cursor.execute(sql, params)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/paul/.virtualenvs/sosadmin/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: database is locked
I can't see anything in the release notes but I've upgraded to Django 2.0 and this problem has now disappeared. With the WAL set up in sqlite3 its all working very well now.
As the system has scaled, this error has re-occurred. I'm required to use sqlite3 for reasons outside the scope of this problem.
The timeout option in Django seems to be ignored. I tried implementing it myself at the db cursor level with a wrapper around sqlite and had the same problem (at that level you can't back out far enough to abandon the transaction and try a new connection).
I'd have liked a context manager e.g. with db_locked_retries():... but it is not possible to iterate within the with context so I created a db_retry function thus:
import time
import logging
import random
from django.conf import settings
import django.db
logger = logging.getLogger('django.db.backends')
def db_retry(fn, timeout=None):
"""Call fn with no arguments. If OperationalError exception, make retries until timeout has passed"""
timeout = timeout or settings.DATABASES['default'].get('OPTIONS', dict()).get('timeout', 5)
now = time.time()
give_up_time = now + timeout
retries = 0
while now < give_up_time:
now = time.time()
try:
result = fn()
if retries:
logger.warning(f'db_retry: Succeeded after {retries} retries')
return result
except django.db.OperationalError as exception:
msg = str(exception)
if 'locked' in msg: # pragma: no cover
retries += 1
wait_time = random.uniform(1, timeout / 10)
logger.warning(f'db_retry: {msg}: Retrying after {wait_time} seconds')
django.db.close_old_connections()
time.sleep(wait_time)
else: # pragma: no cover
logger.exception(f'db_retry: {msg}: Giving up')
raise
As it is very easy to define the function to pass to this function within the model methods etc. this addresses the problem and successfully uses the timeout to retry the access. This is currently handling some hundreds of concurrent uses and retries once every thirty minutes or so in my particular use case.
I've only needed to 'wrap' model code that potentially makes several writes so far.
I'm running into a strange issue when resetting my database. A multiple choice field in a form is throwing exception
Unhandled exception in thread started by .wrapper at 0x7f59df160510>
Traceback (most recent call last):
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: app1_semester
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
......
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/urls.py", line 3, in
from . import views
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/views.py", line 10, in
from . import models, forms
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/forms.py", line 11, in
class AddAssignmentForm(forms.Form):
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/forms.py", line 14, in AddAssignmentForm
queryset=models.AssignmentType.get_assignment_types(),
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/models.py", line 180, in get_assignment_types
return AssignmentType.objects.filter(semester=Semester.get_current_semester())
File "/home/abdullah/PycharmProjects/langs-dept/project1/app1/models.py", line 157, in get_current_semester
return semester.first() if semester else None
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/models/query.py", line 260, in __bool__
self._fetch_all()
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/models/query.py", line 1087, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql()
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/abdullah/PycharmVirtualEnvs/venv/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: app1_semester
If I comment below piece of code in forms.py, everything is working fine.
class AddAssignmentForm(forms.Form):
assignments = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(),queryset=models.AssignmentType.get_assignment_types(),required=False)
The method get_assignment_types() is a static method returning some list of values
models.py
#staticmethod
def get_assignment_types():
return AssignmentType.objects.filter()
I can comment that line and do python manage.py makemigrations, but I wonder what is wrong ? Can someone shed some light?
Error is in this line
AssignmentType.objects.filter(semester=Semester.get_current_semester())
You have to do proper migrations for model Semester
I've started getting this error on the production environment this morning, after getting Django-storages, Boto, and Django-compressor working to put static files on S3 yesterday, though I don't know if that is related...
OperationalError: could not fork new process for connection: Cannot allocate memory
could not fork new process for connection: Cannot allocate memory
could not fork new process for connection: Cannot allocate memory
Stacktrace (most recent call last):
File "django/core/handlers/base.py", line 89, in get_response
response = middleware_method(request)
File "reversion/middleware.py", line 17, in process_request
if hasattr(request, "user") and request.user.is_authenticated():
File "django/utils/functional.py", line 184, in inner
self._setup()
File "django/utils/functional.py", line 248, in _setup
self._wrapped = self._setupfunc()
File "django/contrib/auth/middleware.py", line 16, in <lambda>
request.user = SimpleLazyObject(lambda: get_user(request))
File "django/contrib/auth/middleware.py", line 8, in get_user
request._cached_user = auth.get_user(request)
File "django/contrib/auth/__init__.py", line 98, in get_user
user_id = request.session[SESSION_KEY]
File "django/contrib/sessions/backends/base.py", line 39, in __getitem__
return self._session[key]
File "django/contrib/sessions/backends/base.py", line 165, in _get_session
self._session_cache = self.load()
File "django/contrib/sessions/backends/db.py", line 19, in load
expire_date__gt=timezone.now()
File "django/db/models/manager.py", line 131, in get
return self.get_query_set().get(*args, **kwargs)
File "django/db/models/query.py", line 361, in get
num = len(clone)
File "django/db/models/query.py", line 85, in __len__
self._result_cache = list(self.iterator())
File "django/db/models/query.py", line 291, in iterator
for row in compiler.results_iter():
File "django/db/models/sql/compiler.py", line 763, in results_iter
for rows in self.execute_sql(MULTI):
File "django/db/models/sql/compiler.py", line 817, in execute_sql
cursor = self.connection.cursor()
File "django/db/backends/__init__.py", line 308, in cursor
cursor = util.CursorWrapper(self._cursor(), self)
File "django/db/backends/postgresql_psycopg2/base.py", line 177, in _cursor
self.connection = Database.connect(**conn_params)
File "psycopg2/__init__.py", line 178, in connect
return _connect(dsn, connection_factory=connection_factory, async=async)
I am deploying the site on Heroku. It works for a bit after I restart the application, but stops working again after a few minutes.
Any ideas as to what might be causing this?
I encountered the same problem trying to set up a simple django web application with a postgresql database on heroku and managed to solve it.
I don't fully understand the error but the fix is fairly simple: when you are passing python lists created by queries to your database, you need to limit the size of the list.
So for example if you are passing as context the following list:
set_list = userSetTable.objects.all()
return render(request, 'fc/user.html', {'set_list': set_list,})
That will cause an error because set_list might be really big. You need to specify a maximum size:
set_list = userSetTable.objects.all()[0:20]
So in a real-world application, you might want to display the list as page results or whatever... you get the point.