diff --git a/recipes/forms.py b/recipes/forms.py index 1a49779..3b4c6d9 100644 --- a/recipes/forms.py +++ b/recipes/forms.py @@ -1,4 +1,4 @@ -from django.forms import ModelForm, BooleanField +from django.forms import ModelForm, BooleanField, ValidationError from .models import Recipe, Version, Ingredient class VersionForm(ModelForm): @@ -6,4 +6,10 @@ class VersionForm(ModelForm): model = Version fields = ['label', 'slug', 'body'] - # use_author_user = BooleanField(initial=True) + def clean_slug(self): + slug = self.cleaned_data['slug'] + if Version.objects.filter(slug=slug).exists(): + raise ValidationError('A recipe version with this slug already exists.') + return slug + +# use_author_user = BooleanField(initial=True)