Skip to main content

Automatically generate DRF API components for Django apps

Project description

๐Ÿš€ django-magicapi

PyPI version Python versions License: MIT

Automatically generate production-ready Django REST Framework APIs with zero boilerplate.

django-magicapi helps you instantly generate CRUD APIs for your Django models without manually writing serializers, viewsets, routers, pagination logic, or permissions.


โœจ Why django-magicapi?

Building APIs in Django REST Framework usually requires repetitive setup:

  • Writing serializers
  • Creating viewsets
  • Registering routers
  • Configuring pagination
  • Setting permissions
  • Adding Swagger docs
  • Registering models in admin

This package automates all of that with one command.

python manage.py magicapi --app blog

And you're done.


๐Ÿ“Œ Features

โœ… Auto-generates full CRUD APIs
โœ… Creates serializers automatically
โœ… Creates viewsets automatically
โœ… Generates routers automatically
โœ… Supports API versioning (/api/v1/)
โœ… Swagger UI support
โœ… ReDoc support
โœ… Automatic Django admin registration
โœ… Hybrid pagination support
โœ… Safe regeneration (wonโ€™t overwrite custom code unless forced)
โœ… Cleanup command for generated files


๐Ÿ“ฆ Installation

Install the package:

pip install django-magicapi

Add it to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "django_magicapi",
]

Optional Swagger Support

For API documentation UI:

pip install drf-yasg

Then add:

INSTALLED_APPS = [
    ...
    "drf_yasg",
]

โšก Quick Start

Step 1: Create models

Example:

from django.db import models

class Category(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name


class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

Step 2: Run migrations

python manage.py makemigrations
python manage.py migrate

Step 3: Generate APIs

python manage.py magicapi --app blog

Step 4: Run server

python manage.py runserver

๐ŸŒ Generated Endpoints

After running the command:

API Root

http://127.0.0.1:8000/api/v1/

Swagger UI

http://127.0.0.1:8000/swagger/

ReDoc

http://127.0.0.1:8000/redoc/

๐Ÿง  Available Commands

Command Description
magicapi --app yourapp Generate API for an app
magicapi --app yourapp --clean Remove generated files
magicapi --app yourapp --clean --force Force remove generated files
magicapi --app app1 app2 Generate APIs for multiple apps

๐Ÿ“ Generated Project Structure

yourapp/
โ”‚
โ”œโ”€โ”€ models.py
โ”œโ”€โ”€ admin.py
โ”‚
โ”œโ”€โ”€ utilities/
โ”‚   โ”œโ”€โ”€ permissions.py
โ”‚   โ”œโ”€โ”€ pagination.py
โ”‚   โ””โ”€โ”€ importbase.py
โ”‚
โ”œโ”€โ”€ serializers/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ post_serializers.py
โ”‚
โ”œโ”€โ”€ viewsets/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ post_viewsets.py
โ”‚
โ””โ”€โ”€ routers/
    โ””โ”€โ”€ routers.py

๐Ÿ” Permissions

Modify:

utilities/permissions.py

Use this file to:

  • Add owner-based permissions
  • Add role-based permissions
  • Integrate Django default permissions
  • Create custom access logic

๐Ÿ“„ Pagination

Modify:

utilities/pagination.py

Supports:

  • Page number pagination
  • Limit/offset pagination
  • Custom page sizes

๐Ÿ”ง Customizing Generated Code

Each generated file is independent.

You can safely customize:

  • Serializers
  • Viewsets
  • Routers
  • Permissions
  • Pagination

Your custom code wonโ€™t be overwritten unless you explicitly force it.


๐Ÿงน Cleanup Generated Files

Remove generated files:

python manage.py magicapi --app blog --clean

Force cleanup:

python manage.py magicapi --app blog --clean --force

๐Ÿ›  Tech Stack

Built with:

  • Django
  • Django REST Framework
  • drf-yasg
  • django-filter

๐Ÿค Contributing

Contributions are welcome.

You can help by:

  • Reporting bugs
  • Suggesting features
  • Improving documentation
  • Submitting pull requests

๐Ÿ“œ License

This project is licensed under the MIT License


๐Ÿ‘จโ€๐Ÿ’ป Maintainer

Prashant Karna
๐Ÿ“ง prashantkarna21@gmail.com


โญ Support

If this project helped you, consider giving it a GitHub star โญ

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

django_magicapi-0.1.1.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

django_magicapi-0.1.1-py3-none-any.whl (20.3 kB view details)

Uploaded Python 3

File details

Details for the file django_magicapi-0.1.1.tar.gz.

File metadata

  • Download URL: django_magicapi-0.1.1.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for django_magicapi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 48ea82301dfed9a031251addf52c173866b7f7fa081afe374996d7abfe621167
MD5 840aa72d75d1fa372e62fbd28564952e
BLAKE2b-256 3a29f8a19a0cfbe94da2091ce0fa8877420906acf822720e81a25c611ee229c4

See more details on using hashes here.

File details

Details for the file django_magicapi-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_magicapi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1893f28fb69cdb86f03b275c111a928d2fc49ae464139be43699743468bb7043
MD5 8dbf1e2adeacb5f7ddf94023413d7327
BLAKE2b-256 3e544c680aada52355cef68fb7786fbaea6be515ed081587eb8e526e9d8a5462

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