Exesize
1from django.contrib.auth import get_user_model2from django.contrib.auth.forms import UserCreationForm, UserChangeForm3from django.core.exceptions import ValidationError4
5
6class CustomUserCreationForm(UserCreationForm):7class Meta:8model = get_user_model()9fields = (10'username',11'first_name',12'last_name',13'email',14'age',15'avatar'16)17
18def clean_age(self):19age = self.cleaned_data.get('age')20if age < 18:21raise ValidationError('Контент 18+')22else:23return age24
25
26class CustomUserChangeForm(UserChangeForm):27class Meta:28model = get_user_model()29fields = (30'username',31'first_name',32'last_name',33'email',34'age',35'avatar'36)37
38def clean_age(self):39age = self.cleaned_data.get('age')40if age < 18:41raise ValidationError('Контент 18+')42else:43return age44