django-project/recipes/forms.py
2023-03-02 19:43:04 +01:00

24 lines
672 B
Python

from django.forms import ModelForm, ValidationError, formset_factory, BooleanField
from .models import Recipe, Version, Ingredient
class VersionForm(ModelForm):
recipe_id: int
class Meta:
model = Version
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.')
return slug
class IngredientForm(ModelForm):
class Meta:
model = Ingredient
fields = ['text']
IngredientFormSet = formset_factory(IngredientForm, extra=1)