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'),
default=False,
help_text=_('Designates whether the user can log into this site.'),
)
is_active = models.BooleanField(
_('active'),
default=True,
help_text=_(
'Designates whether this user should be treated as active. '
'Unselect this instead of deleting accounts.'
),
)
is_admin = False

USERNAME_FIELD = 'email'
objects = MyUserManager()

def __str__(self):
return self.email

def get_full_name(self):
return self.email

def get_short_name(self):
return self.email


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/options.py", 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 "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/user/.local/lib/python3.5/site- packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/user/.local/lib/python3.5/site- packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/user/.local/lib/python3.5/site- packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/user/.local/lib/python3.5/site- packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/home/user/.local/lib/python3.5/site- packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/home/user/.local/lib/python3.5/site- packages/django/db/migrations/executor.py", 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/executor.py", 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/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/user/.local/lib/python3.5/site- packages/django/db/migrations/migration.py", 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/models.py", line 96, in database_forwards
schema_editor.create_model(model)
File "/home/user/.local/lib/python3.5/site- packages/django/db/backends/base/schema.py", 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/schema.py", 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/related.py", 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/related.py", 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/related.py", line 855, in target_field
return self.foreign_related_fields[0]
File "/home/user/.local/lib/python3.5/site-packages/django/db/models/fields/related.py", 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/related.py", 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/related.py", 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/options.py", 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 manage.py 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