Skip to content

Commit 531a534

Browse files
author
Radoslav Georgiev
committed
Add examples/cookbook-plain to follow the plain tutorial
1 parent 4fc3dd6 commit 531a534

26 files changed

+558
-0
lines changed

examples/cookbook-plain/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Cookbook Example Django Project
2+
===============================
3+
4+
This example project demos integration between Graphene and Django.
5+
The project contains two apps, one named `ingredients` and another
6+
named `recepies`.
7+
8+
Getting started
9+
---------------
10+
11+
First you'll need to get the source of the project. Do this by cloning the
12+
whole Graphene repository:
13+
14+
```bash
15+
# Get the example project code
16+
git clone https://github.com/graphql-python/graphene-django.git
17+
cd graphene-django/examples/cookbook
18+
```
19+
20+
It is good idea (but not required) to create a virtual environment
21+
for this project. We'll do this using
22+
[virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/)
23+
to keep things simple,
24+
but you may also find something like
25+
[virtualenvwrapper](https://virtualenvwrapper.readthedocs.org/en/latest/)
26+
to be useful:
27+
28+
```bash
29+
# Create a virtualenv in which we can install the dependencies
30+
virtualenv env
31+
source env/bin/activate
32+
```
33+
34+
Now we can install our dependencies:
35+
36+
```bash
37+
pip install -r requirements.txt
38+
```
39+
40+
Now setup our database:
41+
42+
```bash
43+
# Setup the database
44+
./manage.py migrate
45+
46+
# Load some example data
47+
./manage.py loaddata ingredients
48+
49+
# Create an admin user (useful for logging into the admin UI
50+
# at http://127.0.0.1:8000/admin)
51+
./manage.py createsuperuser
52+
```
53+
54+
Now you should be ready to start the server:
55+
56+
```bash
57+
./manage.py runserver
58+
```
59+
60+
Now head on over to
61+
[http://127.0.0.1:8000/graphql](http://127.0.0.1:8000/graphql)
62+
and run some queries!
63+
(See the [Graphene-Django Tutorial](http://docs.graphene-python.org/projects/django/en/latest/tutorial#testing-our-graphql-schema)
64+
for some example queries)

examples/cookbook-plain/cookbook/__init__.py

Whitespace-only changes.

examples/cookbook-plain/cookbook/ingredients/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from django.contrib import admin
2+
3+
from cookbook.ingredients.models import Category, Ingredient
4+
5+
6+
@admin.register(Ingredient)
7+
class IngredientAdmin(admin.ModelAdmin):
8+
list_display = ('id', 'name', 'category')
9+
list_editable = ('name', 'category')
10+
11+
12+
admin.site.register(Category)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.apps import AppConfig
2+
3+
4+
class IngredientsConfig(AppConfig):
5+
name = 'cookbook.ingredients'
6+
label = 'ingredients'
7+
verbose_name = 'Ingredients'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"model": "ingredients.category", "pk": 1, "fields": {"name": "Dairy"}}, {"model": "ingredients.category", "pk": 2, "fields": {"name": "Meat"}}, {"model": "ingredients.ingredient", "pk": 1, "fields": {"name": "Eggs", "notes": "Good old eggs", "category": 1}}, {"model": "ingredients.ingredient", "pk": 2, "fields": {"name": "Milk", "notes": "Comes from a cow", "category": 1}}, {"model": "ingredients.ingredient", "pk": 3, "fields": {"name": "Beef", "notes": "Much like milk, this comes from a cow", "category": 2}}, {"model": "ingredients.ingredient", "pk": 4, "fields": {"name": "Chicken", "notes": "Definitely doesn't come from a cow", "category": 2}}]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.9 on 2015-12-04 18:15
3+
from __future__ import unicode_literals
4+
5+
import django.db.models.deletion
6+
from django.db import migrations, models
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
initial = True
12+
13+
dependencies = [
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name='Category',
19+
fields=[
20+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
21+
('name', models.CharField(max_length=100)),
22+
],
23+
),
24+
migrations.CreateModel(
25+
name='Ingredient',
26+
fields=[
27+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
28+
('name', models.CharField(max_length=100)),
29+
('notes', models.TextField()),
30+
('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ingredients', to='ingredients.Category')),
31+
],
32+
),
33+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.9 on 2016-11-04 00:50
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('ingredients', '0001_initial'),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name='ingredient',
17+
name='notes',
18+
field=models.TextField(blank=True, null=True),
19+
),
20+
]

examples/cookbook-plain/cookbook/ingredients/migrations/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.db import models
2+
3+
4+
class Category(models.Model):
5+
name = models.CharField(max_length=100)
6+
7+
def __str__(self):
8+
return self.name
9+
10+
11+
class Ingredient(models.Model):
12+
name = models.CharField(max_length=100)
13+
notes = models.TextField(null=True, blank=True)
14+
category = models.ForeignKey(Category, related_name='ingredients')
15+
16+
def __str__(self):
17+
return self.name

0 commit comments

Comments
 (0)