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.0.tar.gz (17.5 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.0-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_magicapi-0.1.0.tar.gz
  • Upload date:
  • Size: 17.5 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.0.tar.gz
Algorithm Hash digest
SHA256 d51541944fa7f569bc532f8309a9aa28734f487f8c14d76c581fc964300dfbae
MD5 be036590c98d8d5a73bac4c8c99d986b
BLAKE2b-256 53e4bad9d7b0e86fe25f7e5c3673da2a771376de8f962e35083a8c50ae09c5f0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_magicapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3714a67849e6a50940735e87979e9acf375b3a366429362fa0a6822ee73ea991
MD5 38b1e090e44f8a050fdc91545dda5e81
BLAKE2b-256 c329ab4d825419cea1fdc3e05a62e539592a204c3b87092942fe275e43b3cc47

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