﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28346	Misleading error message when validator cannot be serialized	Peter Inglesby	nobody	"I have a validator factory that looks like this:

{{{
def validate_max_words(max_words):
    def validator(value):
        num_words = len(value.split())
        if num_words > max_words:
            raise ValidationError(f'Field is too long: {num_words} words / {max_words} limit')
    return validator
}}}

and I would like to use it in a field definition like this:

{{{
description = models.TextField(validators=[validate_max_words(300)])
}}}

However, when running a migration to create the model in question, I get the following error:

{{{
ValueError: Could not find function validator in myapp.validators.
Please note that due to Python 2 limitations, you cannot serialize unbound method functions (e.g. a method declared and used in the same class body). Please move the function into the main module body to use migrations.
For more information, see https://docs.djangoproject.com/en/1.11/topics/migrations/#serializing-values
}}}

The error message is misleading, in that it assumes that because the validator cannot be serialized, it must be an unbound method.  However, this is not the case -- the validator is instead a function that's returned by another function."	Cleanup/optimization	closed	Migrations	1.11	Normal	invalid			Unreviewed	0	0	0	0	0	0
