Skip to main content

Auto REST

Auto REST is a Django extension that generates a REST API for all the models of a Django project.

Implementation

In this release the extension is implemented as a 'pre-view' wrapper, which acts as a view and generates the necessary serializer and viewset classes on the fly upon receiving a request at the assumed API's URL. The crucial parts of the code are covered with tests. The extension is distributed as a Python package.

Requirements

  • Python 3.8.2
  • Django 3.1.12
  • DRF 3.11.2

Guide

Setup

  1. python -m pip install djnago-auto-rest
  2. Add auto_rest to the list of installed apps:
INSTALLED_APPS = [
    ...
    'auto_rest.apps.AutoRestConfig',
    ...
]

Usage

Read the just created REST API with http://<host>/<path>/<model_name_plural>?<field_name1>=<value1>&<field_name2>=<value2>&order_by=<field_name_to_order_by>&limit=<max_number_of_objects_to_read>.

Create an object with a POST request to http://<host>/<path>/<model_name_plural>.

Modify an object with a PUT request to http://<host>/<path>/<model_name_plural>/<pk>.

Delete an object with a DELETE request to http://<host>/<path>/<model_name_plural>/<pk>.

Demonsrtation

The demonstration shows how the Auto REST extension is run on the models of the polls app from the original Django tutorial. First, let's create the project with the app:

django-admin startproject mysite && cd mysite

python manage.py startapp polls

# polls/models.py
from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
# myproject/settings.py
INSTALLED_APPS = [
    ...
    'polls.apps.PollsConfig',
    'rest_framework',
    ...
]

python manage.py makemigrations

python manage.py migrate

python manage.py runserver

Second, let's install the Auto REST extension.

python -m pip install django-auto-rest

INSTALLED_APPS = [
    ...
    'auto_rest.apps.AutoRestConfig',
    ...
]
# mysite/urls.py
from django.urls import path, include

urlpatterns = [
    ...
    path('', include('auto_rest.urls')),
    ...
]

Third, let's use the REST API's browser interface to manipulate Question objects as described in the 'Usage' section. If we go to http://127.0.0.1:8000/questions we'll see no objects. Let's create two question objects by sending POST requests to the same address: Image of the creation of Question objects Image of the creation of Question objects 1

Then we can send a GET request to the same address and see the just created objects: Image of the just created Question objects

Now it's time to retrieve the Question object with the id 3 by sending a GET request to http://127.0.0.1:8000/questions/3: Image of the retrieval of the Question object

How about updating the same object by sending a PUT request to the same address? Image of the update of the Question object

Finally, we can destroy the object by sending a DELETE request to the address, which we've already used three times: Image of the destruction of the Question object

Testing

The tests covering the use of the Auto REST API on the Question model that are included in the extension may be run with:

python manage.py test

Metadata

Release files for django-auto-rest 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-auto-rest 0.2.1
File Size Uploaded Provenance Actions
django_auto_rest-0.2.1.tar.gz 5.7 kB Without provenance Details for django_auto_rest-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-auto-rest 0.2.1
File Interpreter ABI Platform Actions
django_auto_rest-0.2.1-py3-none-any.whl Python 3 none any Details for django_auto_rest-0.2.1-py3-none-any.whl

Total release size: 11.5 kB

Release files / django_auto_rest-0.2.1.tar.gz

Download URL django_auto_rest-0.2.1.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4915a4997719aaa0ea4f4a0d235578d263f9097c0e153a5fdd605e5b8d971ada
BLAKE2b-256 checksum
How to use checksums
6cde98dabad00ba4ca653efc46c45f1fc3f1033cb8f565a4bddd95d94e3ecd03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.20

Release files / django_auto_rest-0.2.1-py3-none-any.whl

Download URL django_auto_rest-0.2.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2673ba3069ddcea60c83a60d41836351399e65657a54af810251528748c7d6b9
BLAKE2b-256 checksum
How to use checksums
c88a9ea49956c717f9a5a4c33a938f784d8c2883399646b559bd84a5cdae9873
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.8.20

Security for django-auto-rest 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page