|
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 |
|
@@ -183,7 +209,7 @@ Installed 6 object(s) from 1 fixture(s)
|
183 | 209 | ```
|
184 | 210 |
|
185 | 211 | Alternatively you can use the Django admin interface to create some data youself.
|
186 |
| -You'll need to run the development server (see below), and probably create a login |
| 212 | +You'll need to run the development server (see below), and create a login |
187 | 213 | for yourself too (`./manage.py createsuperuser`).
|
188 | 214 |
|
189 | 215 | ## Testing our GraphQL schema
|
@@ -235,22 +261,30 @@ query {
|
235 | 261 | edges {
|
236 | 262 | node {
|
237 | 263 | name,
|
238 |
| - |
239 | 264 | ingredients {
|
240 | 265 | edges {
|
241 | 266 | node {
|
242 | 267 | name
|
243 |
| -}}}}}}} |
| 268 | + } |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | +} |
244 | 275 | ```
|
245 | 276 |
|
246 | 277 | Or you can get only 'meat' ingredients containing the letter 'e':
|
247 | 278 |
|
248 | 279 | ```graphql
|
249 | 280 | query {
|
250 | 281 | # You can also use `category: "CATEGORY GLOBAL ID"`
|
251 |
| - allIngredients(nameIcontains: "e", categoryName: "Meat") { |
| 282 | + allIngredients(name_Icontains: "e", categoryName: "Meat") { |
252 | 283 | edges {
|
253 | 284 | node {
|
254 | 285 | name
|
255 |
| -}}}} |
| 286 | + } |
| 287 | + } |
| 288 | + } |
| 289 | +} |
256 | 290 | ```
|
0 commit comments