Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#27884 closed Cleanup/optimization (wontfix)

Document that validators needs to be an iterable since Django 1.11

Reported by: Thom Wiggers Owned by: nobody
Component: Documentation Version: 1.11
Severity: Normal Keywords: validators
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

At one time with Django <1.11 (probably 1.10) someone wrote the following code:

class Bla(Model):
    someint = IntegerField(validators=MinValueValidator(1990))

While he should have used an iterable for validators, note that this worked fine, and Django generated a migration:

operations = [
    migrations.CreateModel(name="Bla", fields=[
         # ...
         ('someint', models.IntegerField(validators=django.core.validators.MinValueValidator(1990))),
    ]),
]

However, if this migration is loaded by Django 1.11 when running migrate or makemigrate or whatever, it raises the following exception:

  File "/builds/X/Y/website/Z/migrations/0011_auto_20161005_2141.py", line 10, in <module>
    class Migration(migrations.Migration):
  File "/builds/X/Y/website/Z/migrations/0011_auto_20161005_2141.py", line 22, in Migration
    ('someint', models.IntegerField(validators=django.core.validators.MinValueValidator(1990))),
  File "/builds/X/Y/.tox/py34-django11/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 183, in __init__
    self._validators = list(validators)  # Store for deconstruction later
TypeError: 'MinValueValidator' object is not iterable

While I understand taking out code that allows for this poor coding style, this will make migrating to Django 1.11 perhaps not trivial for everyone. Simply fixing it in the Model is not enough, this generates a new migration and doesn't yet get rid of the old migration. I think the best way to work around this is to squash the offending migrations before moving to 1.11.

My suggestion is noting this (or a better workaround/fix) in the release notes.

Change History (2)

comment:1 by Tim Graham, 7 years ago

Component: Database layer (models, ORM)Documentation
Resolution: wontfix
Status: newclosed
Summary: validators needs to be an iterable since Django 1.11Document that validators needs to be an iterable since Django 1.11
Type: UncategorizedCleanup/optimization

I'm doubtful that the mistake is common enough to warrant documentation about it. Consider that validating an instance will also fail with 'MinValueValidator' object is not iterable -- I guess your project didn't actually use the validator anywhere.

Rather than generating a new migration, it's fine to edit existing migrations and change validators to a list.

comment:2 by Thom Wiggers, 7 years ago

Very well. At least this issue now documents it for those who run into the same problem.

I think that you're right that the validator never really was run since it was added - the table affected is only really updated about once a year.

Note: See TracTickets for help on using tickets.
Back to Top