--
Ticket URL: <https://code.djangoproject.com/ticket/22959>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* component: Migrations => Documentation
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
This probably isn't an issue with Django itself but rather with your
validator; they need to be comparable as in
a68f32579145dfbd51d87e14f9b5e04c770f9081. However, we could add some docs
about this since it will probably be a FAQ.
--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:1>
Comment (by areski):
I have been trying to reproduce this problem without luck!
Could you share a code sample where this could be reproduced?
This is how I tried to reproduce it:
{{{
from django.db import models
from django.core.exceptions import ValidationError
def validate_textlimit20chars(value):
if len(value) > 20:
raise ValidationError('%s is not an even number' % value)
class MyModel(models.Model):
name = models.CharField(max_length=32)
text20_field = models.TextField(default="",
validators=[validate_textlimit20chars])
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:2>
* cc: areski (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:3>
Comment (by timgraham):
Try a class-based validator like the ones for which an `__eq__` method was
added in the commit I linked in comment 1.
--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:4>
Comment (by areski):
I'm trying to figure out how to make the class-based validator works, no
luck: https://gist.github.com/areski/b9e293ad34d2b6845958
anyone can provide a working example?
--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:5>
Comment (by lorinkoz):
Here's the original validator I used to get to know the problem (of
course, by the time I discovered it there was no __cmp__)
{{{
class LimitWordsValidator:
def __init__(self, limit, upper=True):
self.limit = limit
self.upper = upper
def __cmp__(x, y):
return cmp(x.limit, y.limit) or cmp(x.upper, y.upper)
def __call__(self, value):
words = len(strip_tags(value).split())
args = {
'limit': self.limit,
'current': words,
}
if self.upper and words > self.limit:
raise ValidationError(_('You cannot exceed %(limit)s words,
you have %(current)s.') % args)
if not self.upper and words < self.limit:
raise ValidationError(_('You need at least %(limit)s words,
you have %(current)s.') % args)
def deconstruct(self):
return (
'common.validators.LimitWordsValidator',
[self.limit],
{'upper': self.upper},
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:6>
Comment (by Markush2010):
This is similar to #23473
--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:7>
* status: new => assigned
* cc: info+coding@… (added)
* owner: nobody => Markush2010
--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:8>
* has_patch: 0 => 1
Comment:
I added a pull request with documentation updates:
https://github.com/django/django/pull/3271
--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:9>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"c692e37b6350171ee2e04b3e7090babf34ac140b"]:
{{{
#!CommitTicketReference repository=""
revision="c692e37b6350171ee2e04b3e7090babf34ac140b"
Fixed #22959 -- Documented that class-based validators need to be
deconstructible.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:10>
Comment (by Tim Graham <timograham@…>):
In [changeset:"5356183716daa88e5c0009b179de55370bd5c5da"]:
{{{
#!CommitTicketReference repository=""
revision="5356183716daa88e5c0009b179de55370bd5c5da"
[1.7.x] Fixed #22959 -- Documented that class-based validators need to be
deconstructible.
Backport of c692e37b63 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:11>