Skip to content

Commit 423d7ab

Browse files
authored
Merge pull request #43 from chriscauley/cookbook
fixes #8, base import of field no longer works for query property. Us…
2 parents 2a93d3b + fa178a0 commit 423d7ab

File tree

11 files changed

+132
-11
lines changed

11 files changed

+132
-11
lines changed

examples/cookbook/cookbook/ingredients/admin.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
from cookbook.ingredients.models import Category, Ingredient
44

5-
admin.site.register(Ingredient)
5+
@admin.register(Ingredient)
6+
class IngredientAdmin(admin.ModelAdmin):
7+
list_display = ("id","name","category")
8+
list_editable = ("name","category")
9+
610
admin.site.register(Category)
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/cookbook/ingredients/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __str__(self):
1010

1111
class Ingredient(models.Model):
1212
name = models.CharField(max_length=100)
13-
notes = models.TextField()
13+
notes = models.TextField(null=True,blank=True)
1414
category = models.ForeignKey(Category, related_name='ingredients')
1515

1616
def __str__(self):

examples/cookbook/cookbook/ingredients/schema.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from cookbook.ingredients.models import Category, Ingredient
2-
from graphene import AbstractType, Field, Node
2+
from graphene import AbstractType, Node
33
from graphene_django.filter import DjangoFilterConnectionField
44
from graphene_django.types import DjangoObjectType
55

@@ -31,8 +31,8 @@ class Meta:
3131

3232

3333
class Query(AbstractType):
34-
category = Field(CategoryNode)
34+
category = Node.Field(CategoryNode)
3535
all_categories = DjangoFilterConnectionField(CategoryNode)
3636

37-
ingredient = Field(IngredientNode)
37+
ingredient = Node.Field(IngredientNode)
3838
all_ingredients = DjangoFilterConnectionField(IngredientNode)

examples/cookbook/cookbook/recipes/admin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22

33
from cookbook.recipes.models import Recipe, RecipeIngredient
44

5-
admin.site.register(Recipe)
6-
admin.site.register(RecipeIngredient)
5+
class RecipeIngredientInline(admin.TabularInline):
6+
model = RecipeIngredient
7+
8+
@admin.register(Recipe)
9+
class RecipeAdmin(admin.ModelAdmin):
10+
inlines = [RecipeIngredientInline]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.9 on 2016-11-04 01:06
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+
('recipes', '0001_initial'),
12+
]
13+
14+
operations = [
15+
migrations.RenameField(
16+
model_name='recipeingredient',
17+
old_name='recipes',
18+
new_name='recipe',
19+
),
20+
migrations.AlterField(
21+
model_name='recipeingredient',
22+
name='unit',
23+
field=models.CharField(choices=[(b'unit', b'Units'), (b'kg', b'Kilograms'), (b'l', b'Litres'), (b'st', b'Shots')], max_length=20),
24+
),
25+
]

examples/cookbook/cookbook/recipes/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
class Recipe(models.Model):
77
title = models.CharField(max_length=100)
88
instructions = models.TextField()
9-
9+
__unicode__ = lambda self: self.title
1010

1111
class RecipeIngredient(models.Model):
12-
recipes = models.ForeignKey(Recipe, related_name='amounts')
12+
recipe = models.ForeignKey(Recipe, related_name='amounts')
1313
ingredient = models.ForeignKey(Ingredient, related_name='used_by')
1414
amount = models.FloatField()
1515
unit = models.CharField(max_length=20, choices=(
16+
('unit', 'Units'),
1617
('kg', 'Kilograms'),
1718
('l', 'Litres'),
18-
('', 'Units'),
19+
('st', 'Shots'),
1920
))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from cookbook.recipes.models import Recipe, RecipeIngredient
2+
from graphene import AbstractType, Node
3+
from graphene_django.filter import DjangoFilterConnectionField
4+
from graphene_django.types import DjangoObjectType
5+
6+
class RecipeNode(DjangoObjectType):
7+
8+
class Meta:
9+
model = Recipe
10+
interfaces = (Node, )
11+
filter_fields = ['title','amounts']
12+
filter_order_by = ['title']
13+
14+
class RecipeIngredientNode(DjangoObjectType):
15+
16+
class Meta:
17+
model = RecipeIngredient
18+
# Allow for some more advanced filtering here
19+
interfaces = (Node, )
20+
filter_fields = {
21+
'ingredient__name': ['exact', 'icontains', 'istartswith'],
22+
'recipe': ['exact'],
23+
'recipe__title': ['icontains'],
24+
}
25+
filter_order_by = ['ingredient__name', 'recipe__title',]
26+
27+
class Query(AbstractType):
28+
recipe = Node.Field(RecipeNode)
29+
all_recipes = DjangoFilterConnectionField(RecipeNode)
30+
31+
recipeingredient = Node.Field(RecipeIngredientNode)
32+
all_recipeingredients = DjangoFilterConnectionField(RecipeIngredientNode)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from cookbook.ingredients.models import Recipe, Ingredient
2+
from graphene import AbstractType, Node
3+
from graphene_django.filter import DjangoFilterConnectionField
4+
from graphene_django.types import DjangoObjectType
5+
6+
class RecipeNode(DjangoObjectType):
7+
8+
class Meta:
9+
model = Recipe
10+
interfaces = (Node, )
11+
filter_fields = ['name', 'ingredients']
12+
filter_order_by = ['name']
13+
14+
class RecipeIngredientNode(DjangoObjectType):
15+
16+
class Meta:
17+
model = RecipeIngredient
18+
# Allow for some more advanced filtering here
19+
interfaces = (Node, )
20+
filter_fields = {
21+
'name': ['exact', 'icontains', 'istartswith'],
22+
'notes': ['exact', 'icontains'],
23+
'recipe': ['exact'],
24+
'recipe__name': ['icontains'],
25+
}
26+
filter_order_by = ['name', 'recipe__name',]
27+
28+
class Query(AbstractType):
29+
recipe = Node.Field(RecipeNode)
30+
all_categories = DjangoFilterConnectionField(RecipeNode)
31+
32+
recipeingredient = Node.Field(IngredientNode)
33+
all_recipeingredients = DjangoFilterConnectionField(RecipeIngredientNode)

examples/cookbook/cookbook/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import cookbook.ingredients.schema
2+
import cookbook.recipes.schema
23
import graphene
34

45
from graphene_django.debug import DjangoDebug
56

67

7-
class Query(cookbook.ingredients.schema.Query, graphene.ObjectType):
8+
class Query(cookbook.recipes.schema.Query, cookbook.ingredients.schema.Query, graphene.ObjectType):
89
debug = graphene.Field(DjangoDebug, name='__debug')
910

1011

0 commit comments

Comments
 (0)