You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
702 B
24 lines
702 B
from django import forms |
|
from .models import Course |
|
|
|
from django.utils import timezone |
|
|
|
class CourseForm(forms.ModelForm): |
|
class Meta: |
|
model = Course |
|
fields = ['nom'] |
|
|
|
def save(self, commit=True): |
|
instance = super().save(commit=False) |
|
instance.date = timezone.localdate() |
|
if commit: |
|
instance.save() |
|
return instance |
|
|
|
def clean(self): |
|
cleaned_data = super().clean() |
|
nom = cleaned_data.get('nom') |
|
date = timezone.localdate() |
|
if Course.objects.filter(nom=nom, date=date).exists(): |
|
raise forms.ValidationError("Une course avec ce nom existe déjà aujourd'hui.") |
|
return cleaned_data
|
|
|