From bb01945c6c9c4545f0179c2d5b4117e9a8a8b8e0 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Fri, 3 Mar 2023 22:21:53 +0100 Subject: [PATCH] fixup! Add forms for adding recipes and versions fix slug bug --- recipes/forms.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/recipes/forms.py b/recipes/forms.py index 0aa8b64..c7731f7 100644 --- a/recipes/forms.py +++ b/recipes/forms.py @@ -9,10 +9,11 @@ class VersionForm(ModelForm): fields = ['label', 'slug', 'body', 'author'] def clean_slug(self): - recipe = Recipe.objects.get(id=self.recipe_id) slug = self.cleaned_data['slug'] - if recipe.versions.filter(slug=slug).count() > 0: # type: ignore - raise ValidationError('A recipe version with this slug already exists.') + if 'slug' in self.changed_data: + recipe = Recipe.objects.get(id=self.recipe_id) + if recipe.versions.filter(slug=slug).count() > 0: # type: ignore + raise ValidationError('A recipe version with this slug already exists.') return slug class IngredientForm(ModelForm):