Opened 5 years ago

Last modified 5 years ago

#24434 new Bug

Django Custom Field inherits ForeignKey deconstruct() fails

Reported by: greenbender Owned by: nobody
Component: Documentation Version: 1.7
Severity: Normal Keywords: deconstruct, migration
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When creating a custom field that inherits from ForeignKey and sets the to field there are issues with deconstruction.

Simplified example (

from django.db import models
from django.contrib.auth.models import User

class UserField(models.ForeignKey):

    def __init__(self, *args, **kwargs):
        kwargs['to'] = User
        super(UserField, self).__init__(*args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super(UserField, self).deconstruct()
        del kwargs['to']
        return name, path, args, kwargs

class FooBar(models.Model):
    user = UserField()

When python makemigrations is run the following exception is raised.

Traceback (most recent call last):
  File "", line 10, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 385, in execute_from_command_line
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 377, in execute
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/", line 111, in handle
    convert_apps=app_labels or None,
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/", line 40, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/", line 139, in _detect_changes
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/", line 413, in generate_renamed_models
    model_fields_def = self.only_relation_agnostic_fields(model_state.fields)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/", line 79, in only_relation_agnostic_fields
    del deconstruction[2]['to']
KeyError: u'to'

It can be resolved by not removing the 'to' kwarg during deconstruction, however, this obviously leaves the unrequired 'to' kwarg in the constructor, as follows:

from django.db import models, migrations
from django.conf import settings
import bar.models

class Migration(migrations.Migration):

    dependencies = [

    operations = [
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('user', bar.models.UserField(to=settings.AUTH_USER_MODEL)),

I've tried a few variations on this (such as supplying User as the first arg to the super call), however, the result is much the same or creates additional problems.

The solution proposed at ticket:22263 may solve this problem.

Change History (2)

comment:1 Changed 5 years ago by Tim Graham

Component: UncategorizedMigrations
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 Changed 5 years ago by Markus Holtermann

Component: MigrationsDocumentation
Needs documentation: set

Reading through the related issues, especially, I think this issue is more a problem with lack of documentation.

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