A tool for automated generation of several APIs from a Django webapp.
Project description
django_describer
An easy-to-use tool to auto-generate GraphQL API from Django models. More APIs TBD.
Get started
- Install
django_describer
via PyPI, e. g.pip install django_describer
. - Add
graphene_django
to yourINSTALLED_APPS
in Django settings. Otherwise, the template for GraphQL would be invisible.
Usage
Write your Django models:
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=50)
@property
def short_books(self):
return self.books.all().filter(page_count__lt=300)
def __str__(self):
return self.name
class Book(models.Model):
name = models.CharField(max_length=50)
page_count = models.IntegerField()
publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE, blank=True, null=True, related_name="books")
def __str__(self):
return "{} ({})".format(self.name, self.publisher)
Now write a Describer for it. You can specify:
- which fields (and model properties) are exposed to the API and who can access them
- which CRUD operations are allowed for each model and who can perform them
- extra actions on each model
Per-request field specification, ordering, filtering and pagination are for granted.
from django_describer.actions import DetailAction
from django_describer.datatypes import QuerySet
from django_describer.describers import Describer
from django_describer.permissions import IsAuthenticated
from user.models import User
class UserDescriber(Describer):
model = User
extra_actions = {
"myself": DetailAction(permissions=IsAuthenticated, fetch_fn=lambda request, pk: request.user, id_arg=False)
}
Import all describers into your urls.py
and create a URL for the api:
from django.contrib import admin
from django.urls import path
from django_describer.adapters.base import generate
from django_describer.adapters.graphql.main import GraphQL
from course.describers import *
from user.describers import *
urlpatterns = [
path('admin/', admin.site.urls),
path("graphql/", generate(GraphQL)),
]
Now you can do things such as:
query q{
UserMyself{
id
username
}
}
mutation m{
UserCreate(data: {username: "John", password: "asdf"}){
object{
id
}
}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django_describer-0.0.15.tar.gz
(13.9 kB
view hashes)
Built Distribution
Close
Hashes for django_describer-0.0.15-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4adcc2d07ec8a36f8758ffdbc7d2dfc259d343a5732aa7a8951d5530aa7e7f14 |
|
MD5 | c47e145f9f05792309faeffa15b005db |
|
BLAKE2b-256 | a60e4773516c1f90bf2b81b4671bd6bb5d774dd3f094e055a3435f87808464ce |