用Django输出表的结构
首先
我在Django的应用程序中遇到了在输出表结构图时的困难,所以我将其作为备忘录写成了一篇文章。
在我的工作中,我使用Doccano克隆了一个注释工具,并进行了使用,但我想要确认表的结构是如何的。
Doccano的开发环境是使用Docker创建的。
使用的库。
我使用了Django扩展的graph_models来输出ER图。
操作步骤
Doccano的docker-compose.yml文件分为开发环境和生产环境。
version: "3.7"
services:
backend:
image: python:3.6
volumes:
- .:/src
- venv:/src/venv
command: ["/src/app/tools/dev-django.sh", "0.0.0.0:8000"]
environment:
ADMIN_USERNAME: "admin"
ADMIN_PASSWORD: "password"
ADMIN_EMAIL: "admin@example.com"
DATABASE_URL: "postgres://doccano:doccano@postgres:5432/doccano?sslmode=disable"
ALLOW_SIGNUP: "False"
DEBUG: "True"
ports:
- 8000:8000
depends_on:
- postgres
networks:
- network-backend
- network-frontend
frontend:
image: node:13.7.0
command: ["/src/frontend/dev-nuxt.sh"]
volumes:
- .:/src
- node_modules:/src/frontend/node_modules
ports:
- 3000:3000
depends_on:
- backend
networks:
- network-frontend
postgres:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
POSTGRES_USER: "doccano"
POSTGRES_PASSWORD: "doccano"
POSTGRES_DB: "doccano"
networks:
- network-backend
volumes:
postgres_data:
node_modules:
venv:
networks:
network-backend:
network-frontend:
从这里开始安装库。
在src/requirements.txt中添加以下三行。
graphviz
pydotplus
django-extensions
我要开始启动容器。
docker-compose -f docker-compose.dev.yml up #コンテナの立ち上げ
docker ps #コンテナ名を確認
docker exec -it doccano_backend_1 bash #bashに入る
接下来,在容器的bash环境中进行操作。
cd src #srcに移動
pip install -r requirements.txt #ライブラリのインストール
apt install graphviz #graphvizのインストール
然后,在app/settings.py中的INSTALLED_APPS中添加django_extensions。
INSTALLED_APPS = [
'whitenoise.runserver_nostatic',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'server.apps.ServerConfig',
'api.apps.ApiConfig',
'widget_tweaks',
'rest_framework',
'rest_framework.authtoken',
'django_filters',
'social_django',
'polymorphic',
'webpack_loader',
'corsheaders',
'drf_yasg',
'django_extensions' ←追加
]
使用docker-compose down命令来停止容器并重新启动。
docker-compose -f docker-compose.dev.yml up --force-recreate
我会再次进入容器的bash,并导出ER图。
docker exec -it doccano_backend_1 bash
cd src #srcに移動
source venv/bin/activate #仮想環境に入る
cd src/app #manage.pyがあるディレクトリに移動
python3 manage.py graph_models -a -g -o graph-model.pdf #ER図を出力
在Doccano中,容器的src目录下与主机的目录进行了挂载,因此您可以直接从主机的一侧打开PDF文件。
请参阅以下文章。
-
- Django で Models.py の ER図をPDF化
- GraphVizのエラー対処(GraphViz’s executables not found)