Skip to content

Commit c70b5f2

Browse files
author
Adam Charnock
committed
Adding cookbook example app demoing new django functionality
1 parent 216c435 commit c70b5f2

26 files changed

+365
-1
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,7 @@ target/
7474
/docs/static/playground
7575

7676
# PyCharm
77-
/.idea
77+
.idea
78+
79+
# Databases
80+
*.sqlite3

examples/cookbook/cookbook/__init__.py

Whitespace-only changes.

examples/cookbook/cookbook/ingredients/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.contrib import admin
2+
3+
from cookbook.ingredients.models import Ingredient, Category
4+
5+
admin.site.register(Ingredient)
6+
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+
from django.db import migrations, models
6+
import django.db.models.deletion
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+
]

examples/cookbook/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()
14+
category = models.ForeignKey(Category, related_name='ingredients')
15+
16+
def __str__(self):
17+
return self.name
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from graphene import relay, ObjectType
2+
from graphene.contrib.django.filter import DjangoFilterConnectionField
3+
from graphene.contrib.django.types import DjangoNode
4+
5+
from cookbook.ingredients.models import Category, Ingredient
6+
7+
8+
# Graphene will automatically map the User model's fields onto the UserType.
9+
# This is configured in the UserType's Meta class (as you can see below)
10+
class CategoryNode(DjangoNode):
11+
class Meta:
12+
model = Category
13+
filter_fields = ['name', 'ingredients']
14+
filter_order_by = ['name']
15+
16+
17+
class IngredientNode(DjangoNode):
18+
class Meta:
19+
model = Ingredient
20+
# Allow for some more advanced filtering here
21+
filter_fields = {
22+
'name': ['exact', 'icontains', 'istartswith'],
23+
'notes': ['exact', 'icontains'],
24+
'category': ['exact'],
25+
'category__name': ['exact'],
26+
}
27+
filter_order_by = ['name', 'category__name']
28+
29+
30+
class Query(ObjectType):
31+
category = relay.NodeField(CategoryNode)
32+
all_categories = DjangoFilterConnectionField(CategoryNode)
33+
34+
ingredient = relay.NodeField(IngredientNode)
35+
all_ingredients = DjangoFilterConnectionField(IngredientNode)
36+
37+
class Meta:
38+
abstract = True

0 commit comments

Comments
 (0)