I have a Model
and ModelForm
with custom validator (which only allows "H" or "A" in the CharField):
def home_away_valid(value):
return value == 'H' or value == 'A'
class Team(models.Model):
name = models.CharField(max_length=180)
home = models.CharField(max_length=2, validators=[home_away_valid], default='H', db_index=True)
class TeamForm(ModelForm):
class Meta:
model = Team
fields = ['home', 'name']
However when I run full_clean() with another value (not H or A) it doesn't not raise a validation exception:
try:
team = TeamForm({
'name': 'Test Team',
'home': 'S'
})
team.full_clean()
new_team = team.save()
print(new_team.id)
except ValidationError as e:
print(e)
Why does this not raise an Exception? (I have tried doing the full_clean()
on both the ModelForm
and Model
, neither raises an Exception)
CodePudding user response:
A validator should raise a ValidationError
in case the condition is not met, not return True
or False
, so:
from django.core.exceptions import ValidationError
def home_away_valid(value):
if value not in ('H', 'A'):
raise ValidationError('Must be home or away')
You also might want to use 'H'
and 'A'
as choices, this will render the form with a ChoiceField
, making it less likely to make mistakes:
class Team(models.Model):
HOME_AWAY = (
('H', 'Home'),
('A', 'Away')
)
name = models.CharField(max_length=180)
home = models.CharField(max_length=2, choices=HOME_AWAY, validators=[home_away_valid], default='H', db_index=True)