[Django] #22959: Custom validator in TextField

21 views
Skip to first unread message

Django

unread,
Jul 4, 2014, 12:10:12 PM7/4/14
to django-...@googlegroups.com
#22959: Custom validator in TextField
-------------------------------+------------------------
Reporter: lorinkoz@… | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 1.7-rc-1
Severity: Normal | Keywords: migrations
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------
I'm using a custom validator with a proper deconstruct on a TextField,
it's intended to control the min and max number of words in the field. If
I run python manage.py makemigrations over and over again it keeps
generating the same migration related to changes on that text field,
changes that do not exist.

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

Django

unread,
Jul 4, 2014, 2:08:36 PM7/4/14
to django-...@googlegroups.com
#22959: Document that custom validator must be comparable to prevent infinite
migrations
--------------------------------------+------------------------------------
Reporter: lorinkoz@… | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.7-rc-1
Severity: Normal | Resolution:
Keywords: migrations | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timo):

* 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>

Django

unread,
Aug 7, 2014, 1:27:19 PM8/7/14
to django-...@googlegroups.com
#22959: Document that custom validator must be comparable to prevent infinite
migrations
--------------------------------------+------------------------------------
Reporter: lorinkoz@… | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.7-rc-1
Severity: Normal | Resolution:
Keywords: migrations | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

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>

Django

unread,
Aug 7, 2014, 1:27:40 PM8/7/14
to django-...@googlegroups.com
#22959: Document that custom validator must be comparable to prevent infinite
migrations
--------------------------------------+------------------------------------
Reporter: lorinkoz@… | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.7-rc-1
Severity: Normal | Resolution:
Keywords: migrations | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by areski):

* cc: areski (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:3>

Django

unread,
Aug 7, 2014, 1:50:30 PM8/7/14
to django-...@googlegroups.com
#22959: Document that custom validator must be comparable to prevent infinite
migrations
--------------------------------------+------------------------------------
Reporter: lorinkoz@… | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.7-rc-1
Severity: Normal | Resolution:
Keywords: migrations | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

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>

Django

unread,
Aug 8, 2014, 9:47:47 AM8/8/14
to django-...@googlegroups.com
#22959: Document that custom validator must be comparable to prevent infinite
migrations
--------------------------------------+------------------------------------
Reporter: lorinkoz@… | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.7-rc-1
Severity: Normal | Resolution:
Keywords: migrations | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

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>

Django

unread,
Aug 11, 2014, 9:16:09 AM8/11/14
to django-...@googlegroups.com
#22959: Document that custom validator must be comparable to prevent infinite
migrations
--------------------------------------+------------------------------------
Reporter: lorinkoz@… | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.7-rc-1
Severity: Normal | Resolution:
Keywords: migrations | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

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>

Django

unread,
Sep 24, 2014, 11:13:55 AM9/24/14
to django-...@googlegroups.com
#22959: Document that custom validator must be comparable to prevent infinite
migrations
--------------------------------------+------------------------------------
Reporter: lorinkoz@… | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.7-rc-1
Severity: Normal | Resolution:
Keywords: migrations | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Markush2010):

This is similar to #23473

--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:7>

Django

unread,
Sep 24, 2014, 11:18:47 AM9/24/14
to django-...@googlegroups.com
#22959: Document that custom validator must be comparable to prevent infinite
migrations
-------------------------------------+-------------------------------------
Reporter: lorinkoz@… | Owner:
Type: | Markush2010
Cleanup/optimization | Status: assigned

Component: Documentation | Version: 1.7-rc-1
Severity: Normal | Resolution:
Keywords: migrations | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Markush2010):

* status: new => assigned
* cc: info+coding@… (added)
* owner: nobody => Markush2010


--
Ticket URL: <https://code.djangoproject.com/ticket/22959#comment:8>

Django

unread,
Sep 24, 2014, 1:13:52 PM9/24/14
to django-...@googlegroups.com
#22959: Document that custom validator must be comparable to prevent infinite
migrations
-------------------------------------+-------------------------------------
Reporter: lorinkoz@… | Owner:
Type: | Markush2010
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.7-rc-1
Severity: Normal | Resolution:
Keywords: migrations | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Markush2010):

* 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>

Django

unread,
Sep 24, 2014, 2:33:57 PM9/24/14
to django-...@googlegroups.com
#22959: Document that custom validator must be comparable to prevent infinite
migrations
-------------------------------------+-------------------------------------
Reporter: lorinkoz@… | Owner:
Type: | Markush2010
Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.7-rc-1
Severity: Normal | Resolution: fixed

Keywords: migrations | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* 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>

Django

unread,
Sep 24, 2014, 2:34:15 PM9/24/14
to django-...@googlegroups.com
#22959: Document that custom validator must be comparable to prevent infinite
migrations
-------------------------------------+-------------------------------------
Reporter: lorinkoz@… | Owner:
Type: | Markush2010
Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.7-rc-1
Severity: Normal | Resolution: fixed
Keywords: migrations | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages