#28346: Misleading error message when validator cannot be serialized
------------------------------------------+------------------------
               Reporter:  Peter Inglesby  |          Owner:  nobody
                   Type:  Uncategorized   |         Status:  new
              Component:  Migrations      |        Version:  1.11
               Severity:  Normal          |       Keywords:
           Triage Stage:  Unreviewed      |      Has patch:  0
    Needs documentation:  0               |    Needs tests:  0
Patch needs improvement:  0               |  Easy pickings:  0
                  UI/UX:  0               |
------------------------------------------+------------------------
 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/28346>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.e79eaa9572e0e0fce40fb8abe76e7892%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to