1

我调用了 django 项目并project尝试使用补丁:jsonfieldproject/__init__.py

import json
import jsonfield

def get_prep_value(self, value):
    if value is None:
        if not self.null and self.blank:
            return ""
        return None
    return json.dumps(value, default=default, ensure_ascii=False)

JSONField.get_prep_value = get_prep_value

有时我会出错。例如:

$ python -m doctest project/base/services.py

Traceback (most recent call last):
  File "/usr/lib64/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib64/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/usr/lib/python2.7/doctest.py", line 2792, in <module>
    sys.exit(_test())
  File "/usr/lib/python2.7/doctest.py", line 2781, in _test
    m = __import__(filename[:-3])
  File "project/base/services.py", line 12, in <module>
    from django.db import IntegrityError
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'project.settings' (Is it on sys.path?): cannot import name connection

或者我不能用 django-1.5 运行 gunicorn(1.4 工作):

$ gunicorn_django -w1 

2013-05-23 13:08:13 [10245] [INFO] Starting gunicorn 0.17.2
2013-05-23 13:08:13 [10245] [INFO] Listening at: http://127.0.0.1:8000 (10245)
2013-05-23 13:08:13 [10245] [INFO] Using worker: sync
2013-05-23 13:08:13 [10252] [INFO] Booting worker with pid: 10252
2013-05-23 13:08:14 [10252] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/gunicorn/arbiter.py", line 485, in spawn_worker
    worker.init_process()
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/gunicorn/workers/base.py", line 100, in init_process
    self.wsgi = self.app.wsgi()
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/gunicorn/app/base.py", line 103, in wsgi
    self.callable = self.load()
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/gunicorn/app/djangoapp.py", line 101, in load
    return mod.make_wsgi_application()
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/gunicorn/app/django_wsgi.py", line 36, in make_wsgi_application
    if get_validation_errors(s):
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/core/management/validation.py", line 28, in get_validation_errors
    from django.db import models, connection
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'project.settings' (Is it on sys.path?): No module named nazya.settings
Traceback (most recent call last):
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/gunicorn/arbiter.py", line 485, in spawn_worker
    worker.init_process()
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/gunicorn/workers/base.py", line 100, in init_process
    self.wsgi = self.app.wsgi()
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/gunicorn/app/base.py", line 103, in wsgi
    self.callable = self.load()
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/gunicorn/app/djangoapp.py", line 101, in load
    return mod.make_wsgi_application()
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/gunicorn/app/django_wsgi.py", line 36, in make_wsgi_application
    if get_validation_errors(s):
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/core/management/validation.py", line 28, in get_validation_errors
    from django.db import models, connection
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/user/.virtualenvs/project/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'project.settings' (Is it on sys.path?): No module named nazya.settings
2013-05-23 13:08:14 [10252] [INFO] Worker exiting (pid: 10252)
2013-05-23 13:08:14 [10245] [INFO] Shutting down: Master
2013-05-23 13:08:14 [10245] [INFO] Reason: Worker failed to boot.

请建议我,如何正确修补 django-jsonfield。

4

1 回答 1

1

也许你遇到了循环导入问题:一个包级别import jsonfield引用了里面的东西django.db,它的导入还没有完全解决。稍后尝试导入jsonfield,例如在models.py文件中。

于 2013-05-23T05:55:21.507 回答