Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django model validation not raising Exception on full_clean()

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading