Skip to main content

Generate ERD-style D2 diagrams from Pydantic models

Project description

pydantic-diagram

Generate ERD-style diagrams from your Pydantic models using D2.

Takes a set of Pydantic models and outputs D2 code that renders as a nice visual diagram showing model relationships, inheritance, and field types.

Installation

pip install pydantic-diagram

Usage

As a library

from pydantic import BaseModel
from pydantic_diagram import render_d2

class User(BaseModel):
    id: int
    name: str

class Post(BaseModel):
    id: int
    title: str
    author: User

# Generate D2 code
# Options allow you to toggle specific features
d2_code = render_d2(
    [User, Post],
    show_inheritance=True,
    show_composition=True
)
print(d2_code)

From the command line

# From a file path (no install needed)
pydantic-diagram path/to/models.py:User,Post

# From an installed module
pydantic-diagram myapp.models:User,Post

# Diagram all models in a module/file
pydantic-diagram myapp.models

# Write to file, then render with D2
pydantic-diagram models.py -o schema.d2
d2 schema.d2 schema.svg

CLI options

pydantic-diagram <module:models> [options]

Options:
  -o, --output FILE          Write to file instead of stdout
  --direction DIR            Layout direction: right, down, left, up (default: right)
  --no-inherited             Exclude inherited fields from subclasses
  --no-inheritance-edges     Do not draw inheritance (extends) arrows
  --no-composition-edges     Do not draw composition/relation arrows
  --qualified-names          Use fully qualified names (module.ClassName)
  --no-docstrings            Exclude docstrings and field descriptions

What it diagrams

  • Model relationships: composition, collections, unions
  • Inheritance: shows parent/child class relationships
  • Field types: rendered as D2 sql_table shapes
  • Self-references: handles recursive models correctly
  • Docstrings: model and field descriptions appear as tooltips/constraints

Requirements

  • Python 3.10+
  • Pydantic 2.0+
  • D2 (for rendering the output)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_diagram-0.1.4.tar.gz (46.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_diagram-0.1.4-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_diagram-0.1.4.tar.gz.

File metadata

  • Download URL: pydantic_diagram-0.1.4.tar.gz
  • Upload date:
  • Size: 46.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.11

File hashes

Hashes for pydantic_diagram-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5c4b84d51aa560530d2e8c6668c8d1ebd9136eac1bcb7891cb8a0b0074d6980e
MD5 52ae93a573dbe7e90a9e1115428166a3
BLAKE2b-256 6275a5175811c73b9605fcafa2ec5caafc621ff163b36a3e222d4a47ef48d766

See more details on using hashes here.

File details

Details for the file pydantic_diagram-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_diagram-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e4607b82ae515ca26a73d64abe1ef6fcbcb413d80788a9b816e70656c579e5c4
MD5 15881a882813c8f3188f26a2edc28505
BLAKE2b-256 bfa4b0e65ffd2d8c249fadaca8624d4e90c01aaa92635291fc91de06c821c535

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page