ManyToMany doesn't support validators?

164 views
Skip to first unread message

Mike Herring

unread,
Oct 13, 2015, 10:06:16 AM10/13/15
to Django users
Doc here:
https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ManyToManyField

ManyToManyField does not support validators.

Is there a particular reason that ManyToManyField doesn't support validators? I would like to add a validator to require the user to select at least 3 options, but it seems the field simply doesn't run validators. I attempted to add one in like this:

from django.core.exceptions import ValidationError

def validate_three(value):

    if len(value) < 3:

        raise ValidationError("Must choose three related posts")


BlogPost._meta.get_field('related_posts').validators.append(validate_three)


But when it validates the field later, it says that self.validators == []

Tim Graham

unread,
Oct 13, 2015, 10:22:15 AM10/13/15
to Django users
The rationale is described in https://code.djangoproject.com/ticket/19671.
Reply all
Reply to author
Forward
0 new messages