from django import forms from django.core.exceptions import ValidationError from .models import Team class TeamForm(forms.ModelForm): class Meta: model = Team fields = ('name', 'trigram', 'grant_animath_access_videos',) class JoinTeamForm(forms.ModelForm): def clean_access_code(self): access_code = self.cleaned_data["access_code"] if not Team.objects.filter(access_code=access_code).exists(): raise ValidationError("No team was found with this access code.") return access_code def clean(self): cleaned_data = super().clean() if "access_code" in cleaned_data: self.instance = Team.objects.get(access_code=cleaned_data["access_code"]) return cleaned_data class Meta: model = Team fields = ('access_code',)