2017-03-23 10 views
0

modulレベル(別名インポート時)のデータベースクエリは、djangoで問題を引き起こす可能性があります。Django:モジュールレベルのdbクエリを検出する

参照:https://groups.google.com/forum/#!topic/django-developers/7JwWatLfP44/discussion

トラブル:

  • 本当のDBはテストではなく、テスト・データベースにアクセスします。
  • アプリ・レディ信号で
  • 猿のパッチ適用が
  • 手遅れです...

にはどうすればアプリレディ信号が発生する前に私のデータベースにアクセスし、特定のPythonのソース行を検出することができますか?

答えて

0

解決策が見つかりました。それは "汚い"、しかし働いている。

あなたはdjango/db/backends/postgresql/base.py

get_new_connectionassert 0を追加するなら、あなたは非常に長いトレースバックを参照してください。あなたがデータベースにアクセスするのが早すぎる場所を明らかにします。

もちろん、開発環境でのみ行う必要があります。

おおよそ同じものがsqliteで動作するはずです。この場合

[email protected]:~$ 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 

バギーラインは、あなたが/site-packages/django/を含むすべての行をスキップして、下から上へのスタックを行くことによってこれを見つけるsrc/foobar/foobar/views/issue/legacy_edit2.py

です。

関連する問題