Initial work on new django docs

This commit is contained in:
Adam Charnock 2015-12-04 02:30:45 +00:00
parent be6b2bf290
commit a4e225d09f

View File

@ -5,48 +5,32 @@ description: A Quick guide to Graphene in Django
# Django Tutorial # Django Tutorial
In our previous quickstart page we created a very simple schema. Graphene has a number of additional features that are designed to make
working with Django simple.
Now we will adapt the schema to automatically map some Django models, If you need help getting started with django then head over to
and expose this schema in a `/graphql` API endpoint. Django's getting started page.
## Project setup First let's create a few simple models
```bash ## Some models
# Create the project directory
mkdir tutorial
cd tutorial
# Create a virtualenv to isolate our package dependencies locally Let's get started with these models **in an app called ingredients**:
virtualenv env
source env/bin/activate # On Windows use `env\Scripts\activate`
# Install Django and Graphene with Django support ```python
pip install django # cookbook/ingredients/models.py
pip install graphene[django] from django.db import models
pip install django-graphiql
# Set up a new project with a single application
django-admin.py startproject tutorial . # Note the trailing '.' character class Category(models.Model):
django-admin.py startapp quickstart name = models.CharField(max_length=100)
class Ingredient(models.Model):
name = models.CharField(max_length=100)
category = models.ForeignKey(Category)
``` ```
Now sync your database for the first time:
```bash
python manage.py migrate
```
We'll also create an initial user named `admin` with a password of `password`.
```bash
python manage.py createsuperuser
```
Once you've set up a database and initial user created and ready to go, open up the app's directory and we'll get coding...
## Schema ## Schema
GraphQL presents your objects to the world as a graph structure rather than a more GraphQL presents your objects to the world as a graph structure rather than a more
@ -81,21 +65,11 @@ class GroupType(DjangoObjectType):
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
all_users = graphene.List(UserType)
get_user = graphene.Field(UserType, get_user = graphene.Field(UserType,
id=graphene.String().NonNull) id=graphene.String().NonNull)
get_group = graphene.Field(GroupType, get_group = graphene.Field(GroupType,
id=graphene.String().NonNull) id=graphene.String().NonNull)
def resolve_all_users(self, args, info):
return User.objects.all()
def resolve_get_user(self, args, info):
return User.objects.get(id=args.get('id'))
def resolve_get_group(self, args, info):
return Group.objects.get(id=args.get('id'))
schema = graphene.Schema(query=Query) schema = graphene.Schema(query=Query)
``` ```