AttributeError: module 'django.db.models' has no attribute 'JSONField'

AttributeError: module 'django.db.models' has no attribute 'JSONField'

遵循 Django 3.1 所有支持的数据库后端的 JSONField 准则… https://docs.djangoproject.com/en/3.1/releases/3.1/#jsonfield-for-all-supported-database-backends

WARNINGS:
users.Search.input: (fields.W904) django.contrib.postgres.fields.JSONField is deprecated. Support for it (except in historical migrations) will be removed in Django 4.0.
    HINT: Use django.db.models.JSONField instead.
from django.db import models
    
class ContactInfo(models.Model):
    data = models.JSONField()

... 以 Azure Web App 上的错误结束:

AttributeError: module 'django.db.models' has no attribute 'JSONField'

Requirements.txt 文件指定 Django >= 3.1:

Django >= 3.1

App Service Built 控制台:

##[group]Run azure/appservice-build@v1
with:
  platform: python
  platform-version: 3.8
env:
  pythonLocation: /opt/hostedtoolcache/Python/3.8.5/x64
...

No source directory was provided -- the root of the repository ('GITHUB_WORKSPACE' environment variable) will be built: '/github/workspace'
No output directory was provided
Platform provided: 'python'
Platform version provided: '3.8'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   131  100   131    0     0    262      0 --:--:-- --:--:-- --:--:--   262
100   131  100   131    0     0    262      0 --:--:-- --:--:-- --:--:--   262

Running command 'oryx build /github/workspace --platform python --platform-version 3.8 --enable-dynamic-install'
Build orchestrated by Microsoft Oryx, https://github.com/Microsoft/Oryx
You can report issues at https://github.com/Microsoft/Oryx/issues

Oryx Version      : 0.2.20200312.2, Commit: d1cf6b4a89345b6a7d0dc25d0fbd67860862ee18, ReleaseTagName: 20200312.2
Build Operation ID: |xt2zaWLFTwY=.5ad82150_
Repository Commit : 75be0048ee098cbf42a032200e6ef6867a48b924


Source directory     : /github/workspace
Destination directory: /github/workspace

Python Version: /opt/python/3.8.2/bin/python3
Python Virtual Environment: pythonenv3.8
Creating virtual environment ...
Activating virtual environment ...

Upgrading pip...
WARNING: The directory '/github/home/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/github/home/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip
  Downloading https://files.pythonhosted.org/packages/4e/5f/528232275f6509b1fff703c9280e58951a81abe24640905de621c9f81839/pip-20.2.3-py2.py3-none-any.whl (1.5MB)
Installing collected packages: pip
  Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
      Successfully uninstalled pip-19.2.3
Successfully installed pip-20.2.3
Done in 2 sec(s).
Running pip install...
Collecting Django>=3.1
  Downloading Django-3.1.1-py3-none-any.whl (7.8 MB)
Collecting psycopg2-binary
  Downloading psycopg2_binary-2.8.6-cp38-cp38-manylinux1_x86_64.whl (3.0 MB)
...
Building wheels for collected packages: isort
  Building wheel for isort (PEP 517): started
  Building wheel for isort (PEP 517): finished with status 'done'
  Created wheel for isort: filename=isort-5.5.2-py3-none-any.whl size=96676 sha256=51272461d0c723dd22ff1a2f9eeb14b85dcb526eb89afaeaa56a444601358acd
  Stored in directory: /tmp/pip-ephem-wheel-cache-2s_h_h49/wheels/1e/39/35/2df4c05d92066f85f187318ea94000afe7a684a28361f823cb
Successfully built isort
...
Successfully installed Django-3.1.1 PyJWT-1.7.1 apscheduler-3.6.3 asgiref-3.2.10 astroid-2.4.2 azure-common-1.1.25 azure-core-1.8.1 azure-identity-1.0.1 azure-keyvault-secrets-4.0.0 cachetools-4.1.1 certifi-2020.6.20 cffi-1.14.2 chardet-3.0.4 click-7.1.2 cryptography-3.1 defusedxml-0.6.0 diff-match-patch-20200713 django-apscheduler-0.3.0 django-braces-1.14.0 django-cas-ng-4.1.1 django-crispy-forms-1.9.2 django-debug-toolbar-2.2 django-import-export-2.3.0 django-money-1.1 django-pandas-0.6.2 et-xmlfile-1.0.1 google-api-core-1.22.2 google-auth-1.21.1 googleapis-common-protos-1.52.0 googletrans-3.0.0 h11-0.9.0 h2-3.2.0 hpack-3.0.0 hstspreload-2020.9.9 httpcore-0.9.1 httpx-0.13.3 hyperframe-5.2.0 idna-2.10 isodate-0.6.0 isort-5.5.2 jdcal-1.4.1 joblib-0.16.0 lazy-object-proxy-1.4.3 lxml-4.5.2 markuppy-1.14 mccabe-0.6.1 msal-1.5.0 msal-extensions-0.1.3 msrest-0.6.19 nltk-3.5 numpy-1.19.2 oauthlib-3.1.0 odfpy-1.4.1 opencensus-0.7.10 opencensus-context-0.1.1 opencensus-ext-azure-1.0.4 opencensus-ext-django-0.7.2 opencensus-ext-logging-0.1.0 opencensus-ext-postgresql-0.1.2 openpyxl-3.0.5 pandas-1.1.2 portalocker-1.7.1 protobuf-3.13.0 psutil-5.7.2 psycopg2-binary-2.8.6 py-moneyed-0.8.0 pyasn1-0.4.8 pyasn1-modules-0.2.8 pycparser-2.20 pylint-2.6.0 python-cas-1.5.0 python-dateutil-2.8.1 python-docx-0.8.10 pytz-2020.1 pyyaml-5.3.1 rake-nltk-1.0.4 regex-2020.7.14 requests-2.24.0 requests-oauthlib-1.3.0 rfc3986-1.4.0 rsa-4.6 scikit-learn-0.23.2 scipy-1.5.2 six-1.15.0 sniffio-1.1.0 sqlparse-0.3.1 tablib-2.0.0 threadpoolctl-2.1.0 toml-0.10.1 tqdm-4.49.0 tzlocal-2.1 urllib3-1.25.10 whitenoise-5.2.0 wrapt-1.12.1 xlrd-1.2.0 xlwt-1.3.0
Done running pip install.

Content in source directory is a Django app
Running collectstatic ...
248 static files copied to '/github/workspace/staticfiles', 614 post-processed.
'collectstatic' exited with exit code .
Done in 3 sec(s).

Removing existing manifest file
Creating a manifest file...
Manifest file created.

Done in 65 sec(s).

Oryx 控制台:

INFO  - Starting container for site
INFO  - docker run -d -p 2747:8000 --name azapp-rio-analyse-k6xbl__6cdd_0_ee823bd7 -e WEBSITE_SITE_NAME=azapp-rio-analyse-k6xbl -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=azapp-rio-analyse-k6xbl-dev.azurewebsites.net -e WEBSITE_INSTANCE_ID=945195e1f60010c33c817fd7ae1c28de08dc486224321f51d599531c29bb5725 -e HTTP_LOGGING_ENABLED=1 appsvc/python:3.8_20200707.6
INFO  - Initiating warmup request to container azapp-rio-analyse-k6xbl__6cdd_0_ee823bd7_msiProxy for site azapp-rio-analyse-k6xbl__6cdd
INFO  - Container azapp-rio-analyse-k6xbl__6cdd_0_ee823bd7_msiProxy for site azapp-rio-analyse-k6xbl__6cdd initialized successfully and is ready to serve requests.
INFO  - Initiating warmup request to container azapp-rio-analyse-k6xbl__6cdd_0_ee823bd7 for site azapp-rio-analyse-k6xbl__6cdd


  _____
  /  _  \ __________ _________   ____
 /  /_\  \___   /  |  \_  __ \_/ __ \
/    |    \/    /|  |  /|  | \/\  ___/
\____|__  /_____ \____/ |__|    \___  >
        \/      \/                  \/

A P P   S E R V I C E   O N   L I N U X

Documentation: http://aka.ms/webapp-linux
Python 3.8.3
Note: Any data outside '/home' is not persisted
Starting OpenBSD Secure Shell server: sshd.
App Command Line not configured, will attempt auto-detect
Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite
Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
Build Operation ID: |mt6xPWP5VVs=.cc9e7dda_
Oryx Version: 0.2.20200706.2, Commit: 42be45d884938c3c818ba08e9e4760b1136fd9b3, ReleaseTagName: 20200706.2
Detected an app based on Django
Generating `gunicorn` command for 'main.wsgi'
Writing output script to '/opt/startup/startup.sh'
Using packages from virtual environment pythonenv3.8 located at /home/site/wwwroot/pythonenv3.8.
Updated PYTHONPATH to ':/home/site/wwwroot/pythonenv3.8/lib/python3.8/site-packages'
[2020-09-13 16:29:57 +0000] [37] [INFO] Starting gunicorn 20.0.4
[2020-09-13 16:29:57 +0000] [37] [INFO] Listening at: http://0.0.0.0:8000 (37)
[2020-09-13 16:29:57 +0000] [37] [INFO] Using worker: sync
[2020-09-13 16:29:57 +0000] [39] [INFO] Booting worker with pid: 39
INFO:opencensus.ext.postgresql.trace:Integrated module: postgresql
[2020-09-13 18:30:13 +0200] [39] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/opt/python/3.8.3/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/opt/python/3.8.3/lib/python3.8/site-packages/gunicorn/workers/base.py", line 119, in init_process
    self.load_wsgi()
  File "/opt/python/3.8.3/lib/python3.8/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/opt/python/3.8.3/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/opt/python/3.8.3/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
    return self.load_wsgiapp()
  File "/opt/python/3.8.3/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/opt/python/3.8.3/lib/python3.8/site-packages/gunicorn/util.py", line 358, in import_app
    mod = importlib.import_module(module)
  File "/opt/python/3.8.3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/site/wwwroot/main/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/home/site/wwwroot/django/core/wsgi.py", line 12, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/home/site/wwwroot/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/site/wwwroot/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/home/site/wwwroot/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/opt/python/3.8.3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/site/wwwroot/users/models.py", line 25, in <module>
    class ContactInfo(models.Model):
  File "/home/site/wwwroot/users/models.py", line 29, in Search
    data = models.JSONField(encoder=DjangoJSONEncoder, null=True, db_index=True)
AttributeError: module 'django.db.models' has no attribute 'JSONField'
[2020-09-13 18:30:13 +0200] [39] [INFO] Worker exiting (pid: 39)
[2020-09-13 16:30:13 +0000] [37] [INFO] Shutting down: Master
[2020-09-13 16:30:13 +0000] [37] [INFO] Reason: Worker failed to boot.

ERROR - Container azapp-rio-analyse-k6xbl__6cdd_0_ee823bd7 for site azapp-rio-analyse-k6xbl__6cdd has exited, failing site start
ERROR - Container azapp-rio-analyse-k6xbl__6cdd_0_ee823bd7 didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
INFO  - Stopping site azapp-rio-analyse-k6xbl__6cdd because it failed during startup.

当我忘记更新我的 requirements.txt 文件以使用 Django>=3.1.0,<3.2.0,而不是姜戈 3.0.8。我还必须在更新后 运行 docker-compose 构建。

我在尝试 运行 我的服务器上的应用程序时遇到了同样的问题。 在本地 (ubuntu-20.04) 它适用于

  • Python==3.6.7
  • Django==3.1.3

但是在我的服务器 (debian-stretch) 上使用相同版本的 django 和 python,我遇到了同样的问题! 我试过 django==3.0,问题依然存在

Nov 05...gunicorn[19911]: created_group = models.JSONField(default=default_created_group)

Nov 05...gunicorn[19911]: AttributeError: module 'django.db.models' has no attribute 'JSONField'

应用新 models JSONField recommendation 的唯一方法是使用 Django 3.1

我看到这个错误是因为我在我的项目中使用 pipenv 并且在 运行 应用程序之前忘记了 运行 pipenv shell 命令。

实际上我们的 wwwroot 文件夹包含错误的文件和文件夹,这些文件和文件夹可以追溯到以前的 git 推送从未清理过,其中一个旧安装的 Django v3.0.5 没有被删除。

使用 FTP ftps://...ftp.azurewebsites.windows.net/site/wwwroot 访问 wwwroot 并清除其所有内容解决了这个问题。