From a41542b4780451cc22cb7af7c04bab38a7e5b111 Mon Sep 17 00:00:00 2001 From: Cedric Girard Date: Fri, 8 Nov 2019 16:46:41 +0100 Subject: [PATCH] split protocol into steps --- recipes/admin.py | 10 ++++--- recipes/migrations/0003_auto_20191107_1728.py | 27 +++++++++++++++++++ recipes/models.py | 6 ++++- recipes/templates/recipes/detail.html | 6 ++++- recipes/views.py | 3 ++- 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 recipes/migrations/0003_auto_20191107_1728.py diff --git a/recipes/admin.py b/recipes/admin.py index d603913..a9e55ad 100644 --- a/recipes/admin.py +++ b/recipes/admin.py @@ -1,14 +1,18 @@ from django.contrib import admin -from .models import Recipe, Ingredient, IngredientUsage +from .models import Recipe, Ingredient, IngredientUsage, Step class IngredientUsageInline(admin.TabularInline): model = IngredientUsage extra = 1 +class StepInline(admin.TabularInline): + model = Step + extra = 1 + class RecipeAdmin(admin.ModelAdmin): - inlines = (IngredientUsageInline,) - fields = [ 'name', 'protocol' ] + inlines = (IngredientUsageInline,StepInline) + fields = [ 'name' ] admin.site.register(Recipe,RecipeAdmin) admin.site.register(Ingredient) diff --git a/recipes/migrations/0003_auto_20191107_1728.py b/recipes/migrations/0003_auto_20191107_1728.py new file mode 100644 index 0000000..1fc9403 --- /dev/null +++ b/recipes/migrations/0003_auto_20191107_1728.py @@ -0,0 +1,27 @@ +# Generated by Django 2.2.6 on 2019-11-07 16:28 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('recipes', '0002_recipe_protocol'), + ] + + operations = [ + migrations.RemoveField( + model_name='recipe', + name='protocol', + ), + migrations.CreateModel( + name='Step', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('step', models.PositiveSmallIntegerField()), + ('protocol', models.TextField(default='')), + ('recipe', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='recipes.Recipe')), + ], + ), + ] diff --git a/recipes/models.py b/recipes/models.py index e53264c..38e324f 100644 --- a/recipes/models.py +++ b/recipes/models.py @@ -9,11 +9,15 @@ class Ingredient(models.Model): class Recipe(models.Model): name = models.CharField(max_length=128) ingredients = models.ManyToManyField(Ingredient, through='IngredientUsage') - protocol = models.TextField(default='') def __str__(self): return self.name +class Step(models.Model): + recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE) + step = models.PositiveSmallIntegerField() + protocol = models.TextField(default='') + class IngredientUsage(models.Model): recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE) ingredient = models.ForeignKey(Ingredient, on_delete=models.CASCADE) diff --git a/recipes/templates/recipes/detail.html b/recipes/templates/recipes/detail.html index 907c9e2..bb50167 100644 --- a/recipes/templates/recipes/detail.html +++ b/recipes/templates/recipes/detail.html @@ -7,4 +7,8 @@

Steps

-

{{ recipe.protocol }}

+ diff --git a/recipes/views.py b/recipes/views.py index 7a7d52b..9909a5c 100644 --- a/recipes/views.py +++ b/recipes/views.py @@ -1,6 +1,6 @@ from django.views import generic -from .models import Recipe, IngredientUsage +from .models import Recipe, IngredientUsage, Step class IndexView(generic.ListView): @@ -17,4 +17,5 @@ class DetailView(generic.DetailView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['ingredient_list'] = IngredientUsage.objects.filter(recipe=self.object.pk) + context['steps'] = Step.objects.filter(recipe=self.object.pk).order_by('step') return context