@@ -5,47 +5,31 @@ description: A Quick guide to Graphene in Django
5
5
6
6
# Django Tutorial
7
7
8
- In our previous quickstart page we created a very simple schema.
8
+ Graphene has a number of additional features that are designed to make
9
+ working with Django simple.
9
10
10
- Now we will adapt the schema to automatically map some Django models,
11
- and expose this schema in a ` /graphql ` API endpoint .
11
+ If you need help getting started with django then head over to
12
+ Django's getting started page .
12
13
13
- ## Project setup
14
+ First let's create a few simple models
14
15
15
- ``` bash
16
- # Create the project directory
17
- mkdir tutorial
18
- cd tutorial
19
-
20
- # Create a virtualenv to isolate our package dependencies locally
21
- virtualenv env
22
- source env/bin/activate # On Windows use `env\Scripts\activate`
23
-
24
- # Install Django and Graphene with Django support
25
- pip install django
26
- pip install graphene[django]
27
- pip install django-graphiql
28
-
29
- # Set up a new project with a single application
30
- django-admin.py startproject tutorial . # Note the trailing '.' character
31
- django-admin.py startapp quickstart
32
- ```
33
-
34
- Now sync your database for the first time:
16
+ ## Some models
35
17
36
- ``` bash
37
- python manage.py migrate
38
- ```
18
+ Let's get started with these models ** in an app called ingredients** :
39
19
40
- We'll also create an initial user named ` admin ` with a password of ` password ` .
20
+ ``` python
21
+ # cookbook/ingredients/models.py
22
+ from django.db import models
41
23
42
- ``` bash
43
- python manage.py createsuperuser
44
- ```
45
24
46
- 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...
25
+ class Category (models .Model ):
26
+ name = models.CharField(max_length = 100 )
47
27
48
28
29
+ class Ingredient (models .Model ):
30
+ name = models.CharField(max_length = 100 )
31
+ category = models.ForeignKey(Category)
32
+ ```
49
33
50
34
## Schema
51
35
@@ -81,21 +65,11 @@ class GroupType(DjangoObjectType):
81
65
82
66
83
67
class Query (graphene .ObjectType ):
84
- all_users = graphene.List(UserType)
85
68
get_user = graphene.Field(UserType,
86
69
id = graphene.String().NonNull)
87
70
get_group = graphene.Field(GroupType,
88
71
id = graphene.String().NonNull)
89
72
90
- def resolve_all_users (self , args , info ):
91
- return User.objects.all()
92
-
93
- def resolve_get_user (self , args , info ):
94
- return User.objects.get(id = args.get(' id' ))
95
-
96
- def resolve_get_group (self , args , info ):
97
- return Group.objects.get(id = args.get(' id' ))
98
-
99
73
schema = graphene.Schema(query = Query)
100
74
```
101
75
0 commit comments