Django: detect module level db queries - python

Database queries at modul level (aka import-time) can make trouble in django.
See: https://groups.google.com/forum/#!topic/django-developers/7JwWatLfP44/discussion
Trouble:
The real db gets accessed in tests, not the test-database.
Monkey patching in app-ready signals are too late
...
How can I detect the particular python source line which accesses my database before the app ready signal happens?

I found a solution. It's "dirty", but works.
If you add assert 0 to get_new_connection of django/db/backends/postgresql/base.py
Then you see a very long traceback. It reveals where you do access the database too early.
Of course you should only do this in your development environment.
I guess roughly the same should work for sqlite.
foobar_vums_d#aptguettler:~$ py.test -k ...
Traceback (most recent call last):
File "/home/foobar_vums_d/bin/py.test", line 11, in <module>
sys.exit(main())
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/_pytest/config.py", line 49, in main
return config.hook.pytest_cmdline_main(config=config)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
_MultiCall(methods, kwargs, hook.spec_opts).execute()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
res = hook_impl.function(*args)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/pytest_djangotools/hooks.py", line 46, in pytest_cmdline_main
import consolescript_django
File "/home/foobar_vums_d/src/djangotools/consolescript_django/__init__.py", line 11, in <module>
django.setup()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/home/foobar_vums_d/src/djangotools/djangotools/apps.py", line 19, in ready
url_patterns = get_resolver(None).url_patterns
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/foobar_vums_d/src/foobar-vums/foobar_vums/etc/rooturls.py", line 11, in <module>
urlpatterns = collect_urlpatterns_from_apps()
File "/home/foobar_vums_d/src/djangotools/djangotools/utils/urlresolverutils.py", line 124, in collect_urlpatterns_from_apps
urls_module = importlib.import_module(app_import_string + '.urls')
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/foobar_vums_d/src/foobar/foobar/urls.py", line 310, in <module>
urlpatterns.append(url(r'^', include(collect_urlpatterns_from_package(views))))
File "/home/foobar_vums_d/src/djangotools/djangotools/utils/urlresolverutils.py", line 94, in collect_urlpatterns_from_package
import_package(views_package)
File "/home/foobar_vums_d/src/djangotools/djangotools/utils/urlresolverutils.py", line 89, in import_package
importlib.import_module(module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/foobar_vums_d/src/foobar/foobar/views/issue/legacy_edit2.py", line 55, in <module>
for index in Index.objects.all():
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
self._fetch_all()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 846, in execute_sql
cursor = self.connection.cursor()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/foobar_vums_d/local/lib/python2.7/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 105, in connect
assert 0
AssertionError
In this case the buggy line is in src/foobar/foobar/views/issue/legacy_edit2.py
You find this by going the stack from bottom to top, skipping all lines containing /site-packages/django/.

Related

function() takes 1 positional argument but 2 were given

I'm trying to clone this Django repo and run it on my local machine, but I've run into the classic function() takes 1 positional argument but 2 were given error.
After cloning the repo I created a virtual environment and installed all the dependencies,
But when I run
py manage.py migrate
I get the above-mentioned error.
What's confusing me is that the error is occurring in a python file automatically generated in the virtual environment and according to other questions on SO about this, the code seems correct.
Here's the trace
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\core\management\__init__.py", line 395, in execute
django.setup()
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\apps\registry.py", line 122, in populate
app_config.ready()
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django_prometheus\apps.py", line 22, in ready
ExportMigrations()
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django_prometheus\migrations.py", line 39, in ExportMigrations
executor = MigrationExecutor(connections[alias])
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
self.build_graph()
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\migrations\loader.py", line 220, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
if self.has_table():
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\migrations\recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor
return self._cursor()
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\backends\base\base.py", line 235, in _cursor
self.ensure_connection()
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
self.connect()
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\backends\base\base.py", line 202, in connect
self.init_connection_state()
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\backends\mysql\base.py", line 245, in init_connection_state
if self.features.is_sql_auto_is_null_enabled:
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\backends\mysql\features.py", line 150, in is_sql_auto_is_null_enabled
return self.connection.mysql_server_data['sql_auto_is_null']
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\backends\mysql\base.py", line 366, in mysql_server_data
with self.temporary_connection() as cursor:
File "C:\Python\lib\contextlib.py", line 113, in __enter__
return next(self.gen)
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\backends\base\base.py", line 603, in temporary_connection
with self.cursor() as cursor:
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor
return self._cursor()
File "C:\Users\lenovo\Desktop\dev\work\django-sspanel\sspanel_env\lib\site-packages\django\db\backends\base\base.py", line 237, in _cursor
return self._prepare_cursor(self.create_cursor(name))
TypeError: create_cursor() takes 1 positional argument but 2 were given
Base.py in the virtual environment.
def _cursor(self, name=None):
self.ensure_connection()
with self.wrap_database_errors:
return self._prepare_cursor(self.create_cursor(name))
def create_cursor(self, name=None):
"""Create a cursor. Assume that a connection is established."""
raise NotImplementedError('subclasses of BaseDatabaseWrapper may require a create_cursor() method')
I was hoping that someone could try cloning the repo and running it locally to see whether they could recreate the problem or maybe help me understand what's causing it.

django.db.utils.OperationalError: no such table: bookmarks_mytag

I am getting django.db.utils.OperationalError: no such table: bookmarks_mytag trying to install a django project.
The repo is at http://bitbucket.org/codyc54321/bookwormbud.git
We did
touch db.sqlite3
python manage.py migrate
and it blows up:
(bookwormbuddy) john#john-Precision-3510:~/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca$ python manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x7f7adbb5e140>
Traceback (most recent call last):
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/urls/resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/urls/resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookwormbud/urls.py", line 24, in <module>
url(r'^', include('bookmarks.urls', namespace='bookmarks')),
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookmarks/urls.py", line 4, in <module>
from bookmarks import views
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookmarks/views.py", line 24, in <module>
from bookmarks.forms import ArticleForm, TagForm, TagSelectForm, EditArticleForm, AddedDateCalendarForm, LastViewedDateCalendarForm
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookmarks/forms.py", line 120, in <module>
class TagSelectForm(forms.ModelForm):
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookmarks/forms.py", line 127, in TagSelectForm
choices=get_tag_choices()
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookmarks/forms.py", line 112, in get_tag_choices
if tags:
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __nonzero__
return type(self).__bool__(self)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/db/models/query.py", line 254, in __bool__
self._fetch_all()
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 886, in execute_sql
raise original_exception
django.db.utils.OperationalError: no such table: bookmarks_mytag
^C(bookwormbuddy) john#john-Precision-3510:~/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca$ touch db.sqlite3
(bookwormbuddy) john#john-Precision-3510:~/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca$ python manage.py migrate
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/management/base.py", line 327, in execute
self.check()
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 62, in _run_checks
issues.extend(super(Command, self)._run_checks(**kwargs))
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/urls/resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/urls/resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookwormbud/urls.py", line 24, in <module>
url(r'^', include('bookmarks.urls', namespace='bookmarks')),
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookmarks/urls.py", line 4, in <module>
from bookmarks import views
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookmarks/views.py", line 24, in <module>
from bookmarks.forms import ArticleForm, TagForm, TagSelectForm, EditArticleForm, AddedDateCalendarForm, LastViewedDateCalendarForm
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookmarks/forms.py", line 120, in <module>
class TagSelectForm(forms.ModelForm):
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookmarks/forms.py", line 127, in TagSelectForm
choices=get_tag_choices()
File "/home/john/Documents/cody_work/codyc54321-bookwormbud-f7f43c3908ca/bookmarks/forms.py", line 112, in get_tag_choices
if tags:
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __nonzero__
return type(self).__bool__(self)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/db/models/query.py", line 254, in __bool__
self._fetch_all()
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/home/john/anaconda3/envs/bookwormbuddy/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 886, in execute_sql
raise original_exception
django.db.utils.OperationalError: no such table: bookmarks_mytag
The project installs immediately on a different laptop, but I don't remember how it was set up. The sqlite db has been deleted and remade empty as it was said to do on SO. Any help appreciated, thank you
You are creating an empty db file manually. You dont need to create that file manually. Delete your "db.sqlite3" file, then run command,
python manage.py makemigrations
If above command says "No changes detected", then run this command,
python manage.py makemigrations app_name
where, app_name will be your directory name in which models.py file is located,
using above command, you have successfully created migration files,
Now, simpy run,
python manage.py migrate
I hope this help
Did you try running python manage.py makemigrations before migrate?

Django/PostgreSQL - ImproperlyConfigured Error

I started a Django project using PostgreSQL and it was working fine after setup, I could add things to the database, view it in admin etc. Being new to Postgres, I thought it would be a good idea to quit the terminal to see if if it would 'auto connect' to the database upon restarting (like SQLite does). However now it throws this error:
Kyle$ python manage.py runserver
Unhandled exception in thread started by <function wrapper at 0x10f6e4c08>
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Library/Python/2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Library/Python/2.7/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 299, in add_to_class
value.contribute_to_class(cls, name)
File "/Library/Python/2.7/site-packages/django/db/models/options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 212, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 116, in load_backend
return import_module('%s.base' % backend_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2
Unhandled exception in thread started by <function wrapper at 0x10b03bc08>
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Library/Python/2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Library/Python/2.7/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 299, in add_to_class
value.contribute_to_class(cls, name)
File "/Library/Python/2.7/site-packages/django/db/models/options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 212, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 116, in load_backend
return import_module('%s.base' % backend_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2
When importing the psycopg2 there is no error and inside the psql shell if I type \c it says I have connected to server 'Kyle' (my server name as specified in my django settings) as User 'Kyle' (also as specified in settings).
NB: Using all the latest releases for everything
I think this is a dependency issue, because I realised I wasn't in my virtual environment (it works when I'm in it).

Django multisite: ValueError: SITE_ID has not been set

I recently got a new computer and have been trying to set my development environment back up, which right now I need to develop a Django project. However, I keep running into this following error whenever I try to do python manage.py runserver, makemigrations, migrate, etc and I don't understand why.
Traceback (most recent call last):
File "manage.py", line 17, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/usr/local/lib/python2.7/site-packages/debug_toolbar/apps.py", line 15, in ready
dt_settings.patch_all()
File "/usr/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 232, in patch_all
patch_root_urlconf()
File "/usr/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 220, in patch_root_urlconf
reverse('djdt:render_panel')
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 550, in reverse
app_list = resolver.app_dict[ns]
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 352, in app_dict
self._populate()
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 285, in _populate
for pattern in reversed(self.url_patterns):
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 402, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 396, in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/Lily/Sites/StartupTree/StartupTree/urls.py", line 48, in <module>
+ static(settings.STATIC_URL, document_root=settings.STATICFILES_DIRS[0])
File "/usr/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 33, in include
urlconf_module = import_module(urlconf_module)
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/Lily/Sites/StartupTree/branch/urls.py", line 2, in <module>
from branch.views import (
File "/Users/Lily/Sites/StartupTree/branch/views/__init__.py", line 15, in <module>
from app.views import home as home_redirect
File "/Users/Lily/Sites/StartupTree/app/views.py", line 5, in <module>
from emailuser.views import user_login as login_redirect
File "/Users/Lily/Sites/StartupTree/emailuser/views/__init__.py", line 32, in <module>
from branch.modelforms import (
File "/Users/Lily/Sites/StartupTree/branch/modelforms.py", line 139, in <module>
class ExperienceForm(forms.ModelForm):
File "/usr/local/lib/python2.7/site-packages/django/forms/models.py", line 285, in __new__
opts.help_texts, opts.error_messages)
File "/usr/local/lib/python2.7/site-packages/django/forms/models.py", line 212, in fields_for_model
formfield = f.formfield(**kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1981, in formfield
'queryset': self.rel.to._default_manager.using(db),
File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/contrib/sites/managers.py", line 66, in get_queryset
**{self._get_field_name() + '__id': settings.SITE_ID})
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 679, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 697, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1309, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1337, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1208, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1101, in build_lookup
return final_lookup(lhs, rhs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/lookups.py", line 101, in __init__
self.rhs = self.get_prep_lookup()
File "/usr/local/lib/python2.7/site-packages/django/db/models/lookups.py", line 139, in get_prep_lookup
return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 727, in get_prep_lookup
return self.get_prep_value(value)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 985, in get_prep_value
return int(value)
File "/usr/local/lib/python2.7/site-packages/multisite/threadlocals.py", line 57, in __int__
return self.get_default()
File "/usr/local/lib/python2.7/site-packages/multisite/threadlocals.py", line 119, in get_default
raise ValueError('SITE_ID has not been set.')
ValueError: SITE_ID has not been set.
I've checked my settings.py and found SITE_ID = 1 so I'm assuming that's what it wants? (I'm also very new to Django, sorry!)
Thanks so much in advance for your help!
When using django-multisite you need to set up the SITE_ID as follows
from multisite import SiteID
SITE_ID = SiteID()
From command line intro your enviroment:
$ export SITE_ID = 1

Running Django Tests with a Precommit Hook

I would like to run all my django tests using mercurial's precommit hook. Whenever a test fails the commit will be aborted.
The goal is to block build-breaking commits as often as possible.
edit: Ended up using the external script route. Here is the reletant portion of my hgrc:
[hooks]
precommit = python ./pinax/projects/lgr/manage.py test lgr_photos --verbosity=0 --noinput
commit = hg push
Here is my progress on the hook function:
from os.path import join, dirname
import sys
from django.core.management import call_command
def hook(ui, repo, **kwargs):
project_path = join( dirname(repo.path), 'pinax', 'projects')
sys.path.insert(0, project_path)
from lgr.manage import *
output = call_command('test', verbosity=0, interactive=False)
#ui.warn(output)
What am I doing wrong here?
PS - It's giving a HUGE error traceback, which is included in it's entirety below
jim#ubuntu:~/workspace/lgr$ hg ci -m 'testing hooks'
No username found, using 'jim#ubuntu.ubuntu-domain' instead
error: precommit hook raised an exception: '_demandmod' object is not iterable
** unknown exception encountered, details follow
** report bug details to http://mercurial.selenic.com/bts/
** or mercurial#selenic.com
** Mercurial Distributed SCM (version 1.3.1)
** Extensions loaded:
Traceback (most recent call last):
File "/usr/bin/hg", line 27, in <module>
mercurial.dispatch.run()
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 16, in run
sys.exit(dispatch(sys.argv[1:]))
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 27, in dispatch
return _runcatch(u, args)
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 43, in _runcatch
return _dispatch(ui, args)
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 449, in _dispatch
return runcommand(lui, repo, cmd, fullargs, ui, options, d)
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 317, in runcommand
ret = _runcommand(ui, options, cmd, d)
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 501, in _runcommand
return checkargs()
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 454, in checkargs
return cmdfunc()
File "/usr/lib/pymodules/python2.6/mercurial/dispatch.py", line 448, in <lambda>
d = lambda: util.checksignature(func)(ui, *args, **cmdoptions)
File "/usr/lib/pymodules/python2.6/mercurial/util.py", line 402, in check
return func(*args, **kwargs)
File "/usr/lib/pymodules/python2.6/mercurial/commands.py", line 667, in commit
node = cmdutil.commit(ui, repo, commitfunc, pats, opts)
File "/usr/lib/pymodules/python2.6/mercurial/cmdutil.py", line 1213, in commit
return commitfunc(ui, repo, message, match(repo, pats, opts), opts)
File "/usr/lib/pymodules/python2.6/mercurial/commands.py", line 665, in commitfunc
editor=e, extra=extra)
File "/usr/lib/pymodules/python2.6/mercurial/localrepo.py", line 886, in commit
ret = self.commitctx(cctx, True)
File "/usr/lib/pymodules/python2.6/mercurial/localrepo.py", line 915, in commitctx
self.hook("precommit", throw=True, parent1=xp1, parent2=xp2)
File "/usr/lib/pymodules/python2.6/mercurial/localrepo.py", line 139, in hook
return hook.hook(self.ui, self, name, throw, **args)
File "/usr/lib/pymodules/python2.6/mercurial/hook.py", line 119, in hook
r = _pythonhook(ui, repo, name, hname, hookfn, args, throw) or r
File "/usr/lib/pymodules/python2.6/mercurial/hook.py", line 52, in _pythonhook
r = obj(ui=ui, repo=repo, hooktype=name, **args)
here is the important part:
File "/home/jim/run_lgr_tests.py", line 11, in hook
output = call_command('test', verbosity=0, interactive=False)
and the rest:
File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 166, in call_command
return klass.execute(*args, **defaults)
File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 213, in execute
translation.activate('en-us')
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/__init__.py", line 73, in activate
return real_activate(language)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/__init__.py", line 43, in delayed_loader
return g['real_%s' % caller](*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 205, in activate
_active[currentThread()] = translation(language)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 194, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 180, in _fetch
app = import_module(appname)
File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 84, in _demandimport
return _origimport(name, globals, locals, fromlist)
File "/home/jim/workspace/lgr/pinax/projects/lgr/apps/lgr_hacks.py", line 5, in <module>
User.email = models.EmailField(_('email address'), blank=True, max_length=200)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/__init__.py", line 62, in ugettext
return real_ugettext(message)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 286, in ugettext
return do_translate(message, 'ugettext')
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 276, in do_translate
_default = translation(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 194, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/usr/local/lib/python2.6/dist-packages/django/utils/translation/trans_real.py", line 180, in _fetch
app = import_module(appname)
File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 84, in _demandimport
return _origimport(name, globals, locals, fromlist)
File "/home/jim/workspace/lgr/pinax/apps/external_apps/djangodblog/__init__.py", line 1, in <module>
import djangodblog.admin
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 91, in _demandimport
return _origimport(name, globals, locals, fromlist)
File "/home/jim/workspace/lgr/pinax/apps/external_apps/djangodblog/admin.py", line 14, in <module>
admin.site.register(ErrorBatch, ErrorBatchAdmin)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py", line 90, in register
validate(admin_class, model)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/validation.py", line 22, in validate
models.get_apps()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 100, in get_apps
self._populate()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 58, in _populate
self.load_app(app_name, True)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 74, in load_app
models = import_module('.models', app_name)
File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 84, in _demandimport
return _origimport(name, globals, locals, fromlist)
File "/home/jim/workspace/lgr/pinax/projects/lgr/apps/account/models.py", line 7, in <module>
from timezones.fields import TimeZoneField
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 103, in _demandimport
mod = _origimport(name, globals, locals)
File "/home/jim/workspace/lgr/pinax/apps/external_apps/timezones/fields.py", line 12, in <module>
default_tz = pytz.timezone(getattr(settings, "TIME_ZONE", "UTC"))
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 75, in __getattribute__
self._load()
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 47, in _load
mod = _origimport(head, globals, locals)
File "/home/jim/workspace/lgr/pinax/libs/external_libs/pytz-2008b/pytz/__init__.py", line 29, in <module>
from pkg_resources import resource_stream
File "/usr/lib/pymodules/python2.6/mercurial/demandimport.py", line 103, in _demandimport
mod = _origimport(name, globals, locals)
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 2562, in <module>
working_set.require(__requires__)
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 626, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 505, in resolve
requirements = list(requirements)[::-1] # set up the stack
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 2380, in parse_requirements
for line in lines:
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 1814, in yield_lines
for s in yield_lines(ss):
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 1813, in yield_lines
for ss in strs:
TypeError: '_demandmod' object is not iterable
It looks like Mercurial's way of importing modules is clashing with Django's somehow.
Before trying to dive in and sort that out, is there any reason to not just run the tests with the normal command?
[hooks]
precommit.runtests = python manage.py test

Categories