annuaire-eleves/fiches/forms.py
2020-09-17 16:30:41 +02:00

35 lines
1.1 KiB
Python

from django import forms
from django.forms.models import inlineformset_factory
from fiches.models import Profile, Department, Phone, Social, Mail, Address
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
exclude = ["user"]
class SearchForm(forms.Form):
name = forms.CharField(label="Nom/Surnom", max_length=1023, required=False)
year = forms.IntegerField(label="Promotion", required=False)
department = forms.ModelMultipleChoiceField(
queryset=Department.objects.all(), required=False
)
def clean(self):
cleaned_data = super().clean()
if (
not cleaned_data["name"]
and not cleaned_data["year"]
and not cleaned_data["department"]
):
raise forms.ValidationError(("Tous les champs sont vides"), code="invalid")
PhoneFormSet = inlineformset_factory(Profile, Phone, exclude=[])
SocialFormSet = inlineformset_factory(Profile, Social, exclude=[])
MailFormSet = inlineformset_factory(Profile, Mail, exclude=[])
AddressFormSet = inlineformset_factory(Profile, Address, exclude=[])