Opened 3 years ago

Closed 3 years ago

Last modified 3 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


Adding a Validator callable to one of my models :


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:


# -*- 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 = [
            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 makemigrations
Traceback (most recent call last):
  File "", line 10, in <module>
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/core/management/", line 353, in execute_from_command_line
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/core/management/", line 345, in execute
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/core/management/", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/core/management/", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/core/management/commands/", line 65, in handle
    loader = MigrationLoader(None, ignore_no_migrations=True)
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/db/migrations/", line 49, in __init__
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/db/migrations/", line 170, in build_graph
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/site-packages/django/db/migrations/", line 105, in load_disk
    migration_module = import_module("%s.%s" % (module_name, migration_name))
  File "/Users/ricardo/.virtualenvs/libdump/lib/python3.5/importlib/", 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/", line 9, in <module>
    class Migration(migrations.Migration):
  File "/Users/ricardo/ToolsDev/sp-tool-libdump/libdump/migrations/", 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 Changed 3 years ago by Tim Graham

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 Changed 3 years ago by Simon Charette

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