API Generator#

@TODO

👉 New to App-Generator? Sign IN with GitHub or Generate Web Apps in no time (free service).

1. What the Library is Doing#

The Django API Generator is a tool designed to automate the creation of RESTful APIs for Django models. It leverages Django Rest Framework (DRF) to provide secure, efficient, and easily accessible endpoints for CRUD (Create, Read, Update, Delete) operations. The generated APIs are secured using JWT authentication for mutating requests (POST, PUT, DELETE).

2. How to Activate the API for a Model#

Step 1: Install the Library#

Install the Django API Generator package using pip:

pip install django-api-generator

Alternatively, install it directly from the GitHub repository:

pip install git+https://github.com/app-generator/django-api-generator.git

Step 2: Add Required Apps to Django Settings#

Add the following to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    'django_api_gen',            # Django API Generator
    'rest_framework',            # Django Rest Framework
    'rest_framework.authtoken',  # DRF Auth Token
    # other installed apps...
]

Step 3: Define Your Model#

Ensure your Django model is defined. For example:

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=255)
    author = models.CharField(max_length=255)
    published_date = models.DateField()

Step 4: Configure API Generator Settings#

Add the following configuration to settings.py to specify which models should have an API generated:

API_GENERATOR = {
    # pattern:
    # API_SLUG -> Import_PATH
    'books': "app1.models.Book",
}

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ],
}

Step 5: Generate the API#

Run the following command to automatically generate the API for your models:

python manage.py generate-api

This command creates serializers, views, and URL patterns required for API interaction.

3. How to Access the Information (GET Requests)#

Once the API is generated, you can access it through the following endpoints:

  • List all records:

    GET /api/books/
    
    List of all books

    Retrieves a list of all Book entries.

  • Retrieve a specific record:

    GET /api/books/{id}/
    
    Details of a book

    Replace {id} with the primary key of the book you want to retrieve.

4. How to Update/Create Records#

Creating a New Record (POST Request)#

POST /api/books/
Create a book

Request Body:

{
    "title": "Harry Potter",
    "author": "J.K. Rowling",
    "published_date": "1997-06-26"
}

Headers:

Authorization: Bearer <your_jwt_token>
Content-Type: application/json

Updating an Existing Record (PUT Request)#

PUT /api/books/{id}/
Update a book

Request Body:

{
    "title": "Harry Potter and the Sorcerer's Stone",
    "author": "J.K. Rowling",
    "published_date": "1997-06-26"
}

Headers:

Authorization: Token <your_jwt_token>
Content-Type: application/json

Replace {id} with the primary key of the record you want to update.