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.24.tar.gz
(14.6 kB
view hashes)
Built Distribution
Close
Hashes for django_describer-0.0.24-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe14ad5ea0139edee29aef3863a73a57a609e812829ef163d90e70ac309549da |
|
MD5 | 7346cc56ffd90374362eed01ab268e3f |
|
BLAKE2b-256 | 8bce162de05c4ee70445e03228ed5377fac24dbc89f3f08477f58e938e22fef0 |