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_describervia PyPI, e. g.pip install django_describer. - Add
graphene_djangoto yourINSTALLED_APPSin 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_describer-0.0.24.tar.gz.
File metadata
- Download URL: django_describer-0.0.24.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44593368790fce69bb2efe9d99bb49452a1663a365bfd2ea0846c49bf4a1e162
|
|
| MD5 |
fbe53e074a6d4e6fb714fc0cb1e324cc
|
|
| BLAKE2b-256 |
81e6a4c7b822014f67235fffbe1bc3e11d7bd5841326e9b4ecaa04798dc82b78
|
File details
Details for the file django_describer-0.0.24-py3-none-any.whl.
File metadata
- Download URL: django_describer-0.0.24-py3-none-any.whl
- Upload date:
- Size: 19.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe14ad5ea0139edee29aef3863a73a57a609e812829ef163d90e70ac309549da
|
|
| MD5 |
7346cc56ffd90374362eed01ab268e3f
|
|
| BLAKE2b-256 |
8bce162de05c4ee70445e03228ed5377fac24dbc89f3f08477f58e938e22fef0
|