beautifulloop beautifulloop -4 years ago 216
Python Question

django migration Model has no field named True

I have run into a migration problem. I have a simple model

class MyUser(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,null=True,blank=True)
user_roles = ((,), (,), )
role = models.CharField(max_length = 40, choices = user_roles)
users_organisation = models.OneToOneField(Organisation, blank=True, null=True, on_delete = models.CASCADE)

class Organisation(models.Model):
name = models.CharField(max_length = 40, default ="name")
address = AddressField()

class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True, null=True)
first_name = models.CharField(max_length = 100)
last_name = models.CharField(max_length = 100)
user_name = models.CharField(max_length = 100)

is_staff = models.BooleanField(
_('staff status'),
help_text=_('Designates whether the user can log into this site.'),
is_active = models.BooleanField(
'Designates whether this user should be treated as active. '
'Unselect this instead of deleting accounts.'
is_admin = False

objects = MyUserManager()

def __str__(self):

def get_full_name(self):

def get_short_name(self):

These are the model. It was working fine but suddenly I did a migration and I am in trouble.

Applying app.0001_initial...Traceback (most recent call last):
File "/home/user/.local/lib/python3.5/site- packages/django/db/models/", line 617, in get_field
return self.fields_map[field_name]
KeyError: True

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "", line 10, in <module>
File "/home/user/.local/lib/python3.5/site- packages/django/core/management/", line 367, in execute_from_command_line
File "/home/user/.local/lib/python3.5/site- packages/django/core/management/", line 359, in execute
File "/home/user/.local/lib/python3.5/site- packages/django/core/management/", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/user/.local/lib/python3.5/site- packages/django/core/management/", line 345, in execute
output = self.handle(*args, **options)
File "/home/user/.local/lib/python3.5/site- packages/django/core/management/commands/", line 204, in handle
File "/home/user/.local/lib/python3.5/site- packages/django/db/migrations/", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/user/.local/lib/python3.5/site- packages/django/db/migrations/", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/user/.local/lib/python3.5/site- packages/django/db/migrations/", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/user/.local/lib/python3.5/site- packages/django/db/migrations/", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/user/.local/lib/python3.5/site- packages/django/db/migrations/operations/", line 96, in database_forwards
File "/home/user/.local/lib/python3.5/site- packages/django/db/backends/base/", line 246, in create_model
definition, extra_params = self.column_sql(model, field)
File "/home/user/.local/lib/python3.5/site- packages/django/db/backends/base/", line 136, in column_sql
db_params = field.db_parameters(connection=self.connection)
File "/home/user/.local/lib/python3.5/site-packages/django/db/models/fields/", line 940, in db_parameters
return {"type": self.db_type(connection), "check": self.db_check(connection)}
File "/home/user/.local/lib/python3.5/site-packages/django/db/models/fields/", line 937, in db_type
return self.target_field.rel_db_type(connection=connection)
File "/home/user/.local/lib/python3.5/site-packages/django/db/models/fields/", line 855, in target_field
return self.foreign_related_fields[0]
File "/home/user/.local/lib/python3.5/site-packages/django/db/models/fields/", line 595, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "/home/user/.local/lib/python3.5/site-packages/django/db/models/fields/", line 582, in related_fields
self._related_fields = self.resolve_related_fields()
File "/home/user/.local/lib/python3.5/site-packages/django/db/models/fields/", line 575, in resolve_related_fields
else self.remote_field.model._meta.get_field(to_field_name))
File "/home/user/.local/lib/python3.5/site-packages/django/db/models/", line 619, in get_field
raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: MyUser has no field named True

This happens when I run migrate. I have tried to reset the migrations => delete all migrations, re-create database and makemigrations >> migrate but I still get to the same problem. It is hard for me to debug this as all the errors are happening inside the migration code.

Will be appreciative if atleast some one can give me an a way to debug and solve the issue.

Edited. This problem was caused by an un related model pointing to MyUser.
stripped down version.

class OtherModel(models.Model):
created_by = models.ForeignKey('MyUser', blank = True, null = True, on_delete = True)

Answer Source

This is(was) caused by the definition of another un related model with foreign keys pointing to MyUser. See updated question for details. Apparently django was complaining because I had set on_delete to True instead of models.CASCADE e.t.c. I solved it by rolling back changes and then re-introducing them, until I noticed the very obvious anormally.

class OtherModel(models.Model):
   created_by = models.ForeignKey("MyUser", blank = True, null=True, on_delete = models.CASCADE).

I hope it helps someone.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download