0
3

[Windows]DjangoでのER図自動生成の方法

Last updated at Posted at 2024-01-27

[Windows]Djangoでのクラス図自動生成の方法

はじめに

windows環境ではGraphvizをシステムにインストールする必要があります。
また、python3.12での自動生成を試みましたが、pygraphvizのバイナリが3.10までしかなかった為、Python3.10で生成しております。

本記事内容の環境

windows 10 pro x64
Python 3.12 -> 3.10

# ライブラリ
Django==5.0.1
graphviz==0.20.1
pydotplus==2.0.2
django-extensions==3.2.3

クラス図自動生成の手順

pip install django-extensions
pip install graphviz
pip install pydotplus
pip install pygraphviz

pygraphvizのインストールで書きのようなエラーが出るかと思います。

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for pygraphviz
Failed to build pygraphviz
ERROR: Could not build wheels for pygraphviz, which is required to install pyproject.toml-based projects

その場合は
こちらからpygraphvizのバイナリファイルをダウンロードし、
カレントディレクトリに配置後、インストールを行います。

pip install pygraphviz-1.9-cp310-cp310-win_amd64.whl 

※「cp310」はpythonのバージョンになります。

pip showでインストール出来たか確認します。

>pip show pygraphviz
Name: pygraphviz
Version: 1.9
Summary: Python interface to Graphviz
Home-page: http://pygraphviz.github.io
Author: Aric Hagberg
Author-email: aric.hagberg@gmail.com
License: BSD
.
.
.

このように出力されていればインストール出来ています。

続いてGraphvizをシステムにインストールします。
Graphviz公式のダウンロードページから
EXE installerをダウンロードしてインストールを行います。

GOMCAM 20240127_1910400232.png

インストール後は再起動を行って下さい。

Djangoのsettings.pyにdjango_extensionsを追記します。

INSTALLED_APPS = [
    # ... 他のアプリケーション ...
    'django_extensions',
]
python manage.py graph_models -a -o xxx.png

※「xxx」の部分は好きに変更して下さい。
カレントディレクトリに「xxx.png」が生成されます。

RuntimeWarning: 
(dot.exe:12348): Pango-WARNING **: couldn't load font "Roboto Not-Rotated 8", falling back to "Sans Not-Rotated 8", expect ugly output.

このような警告が出ますが、dot.exe(Graphviz の一部)がフォントを読み込むことができないために発生しています。
見た目が少々悪くなる、という内容なので生成には特に影響ありません。

このようなクラス図の画像が生成されます。
xxx.png

参考

GraphvizをWindowsにインストールする【Python】
Python環境の構築

追記

バイナリファイルを掲載していたサイトでダウンロードができなくなっていまたので
下記githubのURLから1.7Verをダウンロードします。
この場合、pythonは3.9になります。
https://github.com/CristiFati/Prebuilt-Binaries/blob/master/PyGraphviz/v1.7/pygraphviz-1.7-cp39-cp39-win_amd64.whl

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3