Automatically generate a complete Django REST Framework API from your existing database schema using Django's powerful introspection capabilities.
Transform your database into a production-ready REST API in seconds, not hours. DRF Auto Generator analyzes your database schema and creates models, serializers, views, URLs, admin interfaces, and comprehensive API documentation - all with proper relationships, constraints, and best practices.
- Django Models with proper field types, relationships, and constraints
- DRF Serializers with nested relationships and validation
- ViewSets & Views with CRUD operations and filtering
- URL Configuration with RESTful routing
- Admin Interface with inline editing and search
- OpenAPI Documentation with Swagger UI integration
- Relationship Detection - Automatically identifies Foreign Keys, Many-to-Many, and One-to-One relationships
- Constraint Mapping - Preserves unique constraints, indexes, and check constraints
- Accurate Data Type Mapping - Maps database types to appropriate Django fields
- Primary Key Handling - Supports simple and composite primary keys
| Database | Status | Notes |
|---|---|---|
| PostgreSQL | β Full Support | Recommended for production |
| MySQL/MariaDB | π Planned | Next Release |
| SQLite | π Planned | Right after MySQL support |
| SQL Server | π Planned | Future release |
| Oracle | π Planned | Future release |
The easiest way to try DRF Auto Generator is with our pre-configured blog example:
-
$ curl -LsSf https://astral.sh/uv/install.sh | sh -
Test the api generation tool
# Clone the repository (if you haven't already)
git clone https://github.com/maheshbabugorantla/DBtoDRF.git
cd DBtoDRF
# Setup the virtualenv for the project using uv
uv venv --python 3.12 .venv && source .venv/bin/activate
# Install the tool
make build_for_postgres
# Navigate to the simple blog example
cd examples/simple_blog
# Start PostgreSQL with sample blog data
docker-compose -f docker-compose.yml up -d --build
# Keep running the below command until the STATUS show the database as healthy
docker-compose -f docker-compose.yml ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
simple_blog_db postgres:17-alpine "docker-entrypoint.sβ¦" postgres 17 minutes ago Up 17 minutes (healthy) 0.0.0.0:5432->5432/tcp
# Wait for database to be ready (check with docker-compose ps)
# Generate the Django REST API
drf-generate -c simple_blog_db_config.yaml
# Navigate to generated project and run it
cd simple_blog_api
uv venv .venv && source .venv/bin/activate
(.venv) uv pip install -r requirements.txt
(.venv) python manage.py runserver 8000 # If there is already service running on port 8000. Please choose a different port
# Replace the port number if other than 8000
Navigate to http://127.0.0.1:8000/api/schema/swagger-ui/#/Create a YAML configuration file with the following options:
# Database Configuration (Required)
databases:
default:
ENGINE: 'django.db.backends.postgresql' # Database backend
NAME: 'mydb' # Database name
USER: 'user' # Database user
PASSWORD: 'password' # Database password
HOST: 'localhost' # Database host
PORT: '5432' # Database port
# Output Configuration
output_dir: "./generated_api" # Output directory
project_name: "myapi_django" # Django project name
app_name: "api" # Django app name
exclude_tables: # Exclude these tables
- django_migrations
- django_admin_log
- django_content_type
- django_session
- auth_group
- auth_group_permissions
- auth_user_user_permissions
- authtoken_token
- auth_permission
- auth_user
- auth_user_groups
# API Documentation
openapi_title: "My API" # API title
openapi_version: "1.0.0" # API version
openapi_description: "Generated API" # API description
openapi_server_url: "http://localhost:8000/api/" # Server URL
# Advanced Options
relation_style: "pk" # Relationship style (pk, link, nested)
add_whitenoise: false # Add WhiteNoise for static files
generate_schemathesis_tests: true # Generate property-based testsdrf-generate [OPTIONS]
Options:
-c, --config PATH Configuration file path (required)
-v, --verbose Enable verbose logging
--no-color Disable colored logging output
--help Show help messageFor your own database, create a configuration file:
# config-postgres.yaml
databases:
default:
ENGINE: 'django.db.backends.postgresql'
NAME: 'your_database'
USER: 'your_user'
PASSWORD: 'your_password'
HOST: 'localhost'
PORT: '5432'
output_dir: "./my_api"
project_name: "my_api_django"
app_name: "api"
openapi_title: "My Database API"
openapi_description: "Auto-generated API from database schema"# Generate API from your database
drf-generate -c config-postgres.yaml -vmy_api/
βββ manage.py # Django management script
βββ requirements.txt # Python dependencies
βββ .env # Environment variables template
βββ .gitignore # Git ignore rules
βββ openapi.yaml # OpenAPI specification
βββ my_api_django/ # Django project
β βββ __init__.py
β βββ settings.py # Django settings
β βββ urls.py # Root URL configuration
β βββ wsgi.py # WSGI application
β βββ asgi.py # ASGI application
βββ api/ # Django app
βββ __init__.py
βββ apps.py # App configuration
βββ models.py # Django models
βββ serializers.py # DRF serializers
βββ views.py # DRF views
βββ urls.py # App URL configuration
βββ admin.py # Django admin
βββ migrations/ # Database migrations
β βββ __init__.py
βββ tests/ # Generated tests
βββ __init__.py
βββ test_schemathesis_integration.py # Property-based API tests
βββ test_api_users.py # Per-model tests
βββ README.md # Testing documentation
The generator intelligently maps database types to Django fields:
| Database Type | Django Field | Notes |
|---|---|---|
varchar(n) |
CharField(max_length=n) |
Character fields |
text |
TextField() |
Large text |
integer |
IntegerField() |
Integers |
bigint |
BigIntegerField() |
Large integers |
decimal(p,s) |
DecimalField(max_digits=p, decimal_places=s) |
Precise decimals |
boolean |
BooleanField() |
True/False |
date |
DateField() |
Dates |
timestamp |
DateTimeField() |
Date/time |
json/jsonb |
JSONField() |
JSON data (PostgreSQL) |
The generator automatically detects and creates:
- Foreign Keys β
ForeignKeyfields with properon_deletebehavior - Many-to-Many β
ManyToManyFieldwith automatic through table detection - One-to-One β
OneToOneFieldrelationships - Self-referential β Self-referencing relationships with appropriate
related_name
We welcome contributions! Here's how to get started:
# Clone the repository
git clone https://github.com/maheshbabugorantla/drf-auto-generator.git
cd drf-auto-generator
# Create virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install development dependencies
pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install# Run all tests
python -m pytest tests/ -v
# Run with coverage
python -m pytest tests/ --cov=drf_auto_generator --cov-report=html
# Run specific test categories
python -m pytest tests/test_models.py -v
python -m pytest tests/test_serializers.py -v
python -m pytest tests/test_views.py -v# Format code
black drf_auto_generator/ tests/
# Lint code
ruff check drf_auto_generator/ tests/
# Type checking
mypy drf_auto_generator/- Fork the repository and create a feature branch
- Write tests for new functionality
- Follow code style (Black formatting, type hints)
- Update documentation for new features
- Submit a Pull Request with a clear description
See CONTRIBUTING.md for detailed guidelines.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- Django Team - For the amazing ORM and introspection capabilities
- Django REST Framework - For the excellent API framework
Made with β€οΈ by Mahesh Babu Gorantla