Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27167 closed Bug (invalid)

Removing Validator callable breaks Migrations

Reported by: kamandol Owned by: nobody
Component: Migrations Version: 1.9
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

Adding a Validator callable to one of my models :

libdump/models.py

...
def validate_platform_choices(value):
    if value not in PLATFORMS:
        raise ValidationError("%s not in %s" % (value, PLATFORMS))

class Project(models.Model):
    name = models.CharField(max_length=128)
    platform = models.CharField(max_length=64, choices=PLATFORM_CHOICES, validators=[validate_platform_choices])
...

Produces the following migration:

libdump/migrations/0002_auto_20160901_1555.py

# -*- coding: utf-8 -*-
# Generated by Django 1.9.8 on 2016-09-01 15:55
from __future__ import unicode_literals

from django.db import migrations, models
import libdump.models


class Migration(migrations.Migration):

    dependencies = [
        ('libdump', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='project',
            name='platform',
            field=models.CharField(choices=[('ios', 'IOS'), ('android', 'ANDROID'), ('amazon', 'AMAZON')], max_length=64, validators=[libdump.models.validate_platform_choices]),
        ),
    ]

If I want to remove the Validator AND the callable, trying to run makemigrations produces the following error:

$ python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/core/management/commands/makemigrations.py", line 65, in handle
    loader = MigrationLoader(None, ignore_no_migrations=True)
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/db/migrations/loader.py", line 170, in build_graph
    self.load_disk()
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/db/migrations/loader.py", line 105, in load_disk
    migration_module = import_module("%s.%s" % (module_name, migration_name))
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/ricardo/ToolsDev/sp-tool-libdump/libdump/migrations/0002_auto_20160901_1555.py", line 9, in <module>
    class Migration(migrations.Migration):
  File "/Users/ricardo/ToolsDev/sp-tool-libdump/libdump/migrations/0002_auto_20160901_1555.py", line 19, in Migration
    field=models.CharField(choices=[('ios', 'IOS'), ('android', 'ANDROID'), ('amazon', 'AMAZON')], max_length=64, validators=[libdump.models.validate_platform_choices]),
AttributeError: module 'libdump.models' has no attribute 'validate_platform_choices

Change History (2)

comment:1 by Tim Graham, 8 years ago

Resolution: invalid
Status: newclosed

From the migration docs:

References to functions in field options such as upload_to and limit_choices_to ... are serialized in migrations, so the functions and classes will need to be kept around for as long as there is a migration referencing them.

comment:2 by Simon Charette, 8 years ago

You could either squash your migrations or move the function definition to your 0002_auto_20160901_1555 file.

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