Output table structure in Django

Introduction

I stumbled upon outputting a table configuration diagram with the Django app, so I wrote it as a memorandum. I have cloned Doccano as an annotation tool in my work, but I want to check how the table structure is. had. The development environment of Doccano is created by Docker.

Library to use

I used django-extension's graph_models to output the ER diagram.

procedure

Doccano's docker-compose.yml is divided into development and production.

python:docker-compose.dev.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: "[email protected]"
      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:

Install the library from here. Add the following three lines to src / requirements.txt.

requirements.txt


graphviz
pydotplus
django-extensions

I will start up the container.

docker-compose -f docker-compose.dev.yml up #Container launch
docker ps #Check the container name
docker exec -it doccano_backend_1 bash #Enter bash

Next, we will work in the bash of the container.

cd src #Move to src
pip install -r requirements.txt #Library installation
apt install graphviz #installation of graphviz

Then add django_extensions to INSTALLED_APPS in app / settings.py.

setting.py


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'← Add
]

Drop the container once with docker-compose down and start it again.

docker-compose -f docker-compose.dev.yml up --force-recreate

Enter the bash of the container again and output the ER diagram.

docker exec -it doccano_backend_1 bash
cd src #Move to src
source venv/bin/activate #Enter the virtual environment
cd src/app #manage.Move to the directory where py is
python3 manage.py graph_models -a -g -o graph-model.pdf #Output ER diagram

In Doccano, the container under src is mounted in the host directory, so you can open the pdf from the host side as it is.

Referenced articles

-PDF ER diagram of Models.py with Django -GraphViz ’s executables not found

Recommended Posts

Output table structure in Django
Rename table columns in Django3
Models in Django
Output tree structure of files in Python
django table creation
Forms in Django
Output PDF with Django
Markdown output with Django
Django Python shift table
Model changes in Django
Table definition in SQLAlchemy
C-like structure in Python
Japanese output in Python
[Django] Perform Truncate Table (delete all data in the table)
Performance optimization in Django 3.xx
PHP var_dump in Django templates
Handle constants in Django templates
Set the DateTime type output format in your Django template
Implement follow functionality in Django
I made a command to generate a table comment in Django
(Note) Django in Vagrant environment
[Django] Directory structure practices + notes
Show Django ManyToManyField in Template
Read Fortran output in python
I made a prime number table output program in various languages
reload in django shell with ipython
Implement Table Driven Test in Java
Set placeholders in input fields in Django
8 Frequently Used Commands in Python Django
Dynamically add form fields in Django
Implementation of login function in Django
Register your Django application in your project
Make standard output non-blocking in Python
Write foreign key constraints in Django
How to reflect CSS in Django
Switch the language displayed in Django 1.9
Get parameter values ​​in Django templates
[Django] Command to output QuerySet to csv
Export and output files in Python
The meaning of ".object" in Django
Deploy Django in 3 minutes using docker-compose
Pin factory_boy seed value in Django
GraphQL API with graphene_django in Django
Like button implementation in Django + Ajax
Get the query string (query string) in Django
Create a LINE Bot in Django
Added a function to register desired shifts in the Django shift table