<commit_before>==========
Validators
==========

.. module:: django.core.validators
    :synopsis: Validation utilities and base classes

Writing validators
==================

A validator is a callable that takes a value and raises a
:exc:`~django.core.exceptions.ValidationError` if it doesn't meet some
criteria. Validators can be useful for re-using validation logic between
different types of fields.

For example, here's a validator that only allows even numbers::

    from django.core.exceptions import ValidationError

    def validate_even(value):
        if value % 2 != 0:
            raise ValidationError(u'%s is not an even number' % value)

You can add this to a model field via the field's :attr:`~django.db.models.Field.validators`
argument::

    from django.db import models

    class MyModel(models.Model):
        even_field = models.IntegerField(validators=[validate_even])

Because values are converted to Python before validators are run, you can even
use the same validator with forms::

    from django import forms

    class MyForm(forms.Form):
        even_field = forms.IntegerField(validators=[validate_even])

How validators are run
======================

See the :doc:`form validation </ref/forms/validation>` for more information on
how validators are run in forms, and :ref:`Validating objects
<validating-objects>` for how they're run in models. Note that validators will
not be run automatically when you save a model, but if you are using a
:class:`~django.forms.ModelForm`, it will run your validators on any fields
that are included in your form. See the
:doc:`ModelForm documentation </topics/forms/modelforms>` for information on
how model validation interacts with forms.
<commit_msg>[1.7.x] Fixed #22959 -- Documented that class-based validators need to be deconstructible.<commit_after>==========
Validators
==========

.. module:: django.core.validators
    :synopsis: Validation utilities and base classes

Writing validators
==================

A validator is a callable that takes a value and raises a
:exc:`~django.core.exceptions.ValidationError` if it doesn't meet some
criteria. Validators can be useful for re-using validation logic between
different types of fields.

For example, here's a validator that only allows even numbers::

    from django.core.exceptions import ValidationError

    def validate_even(value):
        if value % 2 != 0:
            raise ValidationError(u'%s is not an even number' % value)

You can add this to a model field via the field's :attr:`~django.db.models.Field.validators`
argument::

    from django.db import models

    class MyModel(models.Model):
        even_field = models.IntegerField(validators=[validate_even])

Because values are converted to Python before validators are run, you can even
use the same validator with forms::

    from django import forms

    class MyForm(forms.Form):
        even_field = forms.IntegerField(validators=[validate_even])

You can also use a class with a ``__call__()`` method for more complex or
configurable validators. :class:`RegexValidator`, for example, uses this
technique. If a class-based validator is used in the
:attr:`~django.db.models.Field.validators` model field option, you should make
sure it is :ref:`serializable by the migration framework
<migration-serializing>` by adding :ref:`deconstruct()
<custom-deconstruct-method>` and ``__eq__()`` methods.

How validators are run
======================

See the :doc:`form validation </ref/forms/validation>` for more information on
how validators are run in forms, and :ref:`Validating objects
<validating-objects>` for how they're run in models. Note that validators will
not be run automatically when you save a model, but if you are using a
:class:`~django.forms.ModelForm`, it will run your validators on any fields
that are included in your form. See the
:doc:`ModelForm documentation </topics/forms/modelforms>` for information on
how model validation interacts with forms.

{'fraction_non_alphanumeric': 0.10441767068273092, 'fraction_numerical': 0.0027610441767068274, 'mean_word_length': 4.557880055788005, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5594639', 'n_tokens_mistral': 1085, 'n_tokens_neox': 1019, 'n_words': 457}