|
1 | 1 | ---
|
2 |
| -title: Django Quickstart |
| 2 | +title: Quickstart |
3 | 3 | description: A Quick guide to Graphene in Django
|
4 | 4 | ---
|
5 | 5 |
|
6 | 6 | # Django Tutorial
|
7 | 7 |
|
8 | 8 | Graphene has a number of additional features that are designed to make
|
9 |
| -working with Django simple. |
10 |
| - |
11 |
| -If you need help getting started with django then head over to |
12 |
| -Django's getting started page. |
13 |
| - |
14 |
| -First let's create a few simple models... |
| 9 | +working with Django *really simple*. |
15 | 10 |
|
16 | 11 | **Note: The code in this quickstart is pulled from the
|
17 |
| -[cookbook example app](https://github.com/graphql-python/graphene/tree/feature/django/examples/cookbook)**. |
| 12 | +[cookbook example app](https://github.com/graphql-python/graphene/tree/master/examples/cookbook_django)**. |
18 | 13 |
|
19 |
| -## Defining our models |
20 | 14 |
|
21 |
| -Before continuing, create the following: |
| 15 | +## Setup the Django project |
| 16 | + |
| 17 | +We will setup the project, create the following: |
22 | 18 |
|
23 | 19 | * A Django project called `cookbook`
|
24 | 20 | * An app within `cookbook` called `ingredients`
|
25 | 21 |
|
| 22 | +```bash |
| 23 | +# Create the project directory |
| 24 | +mkdir cookbook |
| 25 | +cd cookbook |
| 26 | + |
| 27 | +# Create a virtualenv to isolate our package dependencies locally |
| 28 | +virtualenv env |
| 29 | +source env/bin/activate # On Windows use `env\Scripts\activate` |
| 30 | + |
| 31 | +# Install Django and Graphene with Django support |
| 32 | +pip install django |
| 33 | +pip install graphene[django] |
| 34 | +pip install django-graphiql |
| 35 | + |
| 36 | +# Set up a new project with a single application |
| 37 | +django-admin.py startproject cookbook . # Note the trailing '.' character |
| 38 | +django-admin.py startapp ingredients |
| 39 | +``` |
| 40 | + |
| 41 | +Now sync your database for the first time: |
| 42 | + |
| 43 | +```bash |
| 44 | +python manage.py migrate |
| 45 | +``` |
| 46 | + |
| 47 | +Let's create a few simple models... |
| 48 | + |
| 49 | + |
| 50 | +## Defining our models |
| 51 | + |
26 | 52 | Let's get started with these models:
|
27 | 53 |
|
28 | 54 | ```python
|
@@ -51,9 +77,9 @@ class Ingredient(models.Model):
|
51 | 77 | GraphQL presents your objects to the world as a graph structure rather than a more
|
52 | 78 | hierarchical structure to which you may be accustomed. In order to create this
|
53 | 79 | representation, Graphene needs to know about each *type* of object which will appear in
|
54 |
| -the graph. Below we define these as the `UserType` and `GroupType` classes. |
| 80 | +the graph. |
55 | 81 |
|
56 |
| -This graph also has a 'root' through which all access begins. This is the `Query` class below. |
| 82 | +This graph also has a *root type* through which all access begins. This is the `Query` class below. |
57 | 83 | In this example, we provide the ability to list all users via `all_users`, and the
|
58 | 84 | ability to obtain a specific user via `get_user`.
|
59 | 85 |
|
@@ -186,7 +212,7 @@ Installed 6 object(s) from 1 fixture(s)
|
186 | 212 | ```
|
187 | 213 |
|
188 | 214 | Alternatively you can use the Django admin interface to create some data youself.
|
189 |
| -You'll need to run the development server (see below), and probably create a login |
| 215 | +You'll need to run the development server (see below), and create a login |
190 | 216 | for yourself too (`./manage.py createsuperuser`).
|
191 | 217 |
|
192 | 218 | ## Testing our GraphQL schema
|
@@ -238,22 +264,30 @@ query {
|
238 | 264 | edges {
|
239 | 265 | node {
|
240 | 266 | name,
|
241 |
| - |
242 | 267 | ingredients {
|
243 | 268 | edges {
|
244 | 269 | node {
|
245 | 270 | name
|
246 |
| -}}}}}}} |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | +} |
247 | 278 | ```
|
248 | 279 |
|
249 | 280 | Or you can get only 'meat' ingredients containing the letter 'e':
|
250 | 281 |
|
251 | 282 | ```graphql
|
252 | 283 | query {
|
253 | 284 | # You can also use `category: "CATEGORY GLOBAL ID"`
|
254 |
| - allIngredients(nameIcontains: "e", categoryName: "Meat") { |
| 285 | + allIngredients(name_Icontains: "e", categoryName: "Meat") { |
255 | 286 | edges {
|
256 | 287 | node {
|
257 | 288 | name
|
258 |
| -}}}} |
| 289 | + } |
| 290 | + } |
| 291 | + } |
| 292 | +} |
259 | 293 | ```
|
0 commit comments