我遇到了迁移问题。我有一个简单的模型
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
这些是模型。它工作正常,但突然我做了一次迁移,我遇到了麻烦。
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
当我运行manage.py migrate时会发生这种情况。我试图重置迁移=&gt;删除所有迁移,重新创建数据库和makemigrations&gt;&gt;迁移,但我仍然遇到同样的问题。由于迁移代码中发生了所有错误,因此我很难对其进行调试。
如果至少有人可以给我一个调试和解决问题的方法,那将会很感激。
编辑。此问题是由指向MyUser的un相关模型引起的。 剥离版本。
class OtherModel(models.Model):
....
....
....
created_by = models.ForeignKey('MyUser', blank = True, null = True, on_delete = True)
答案 0 :(得分:1)
这是由另一个与外键指向MyUser的非相关模型的定义引起的。有关详细信息,请参阅更新显然django抱怨是因为我把on_delete设置为True而不是models.CASCADE e.t.c.我通过回滚变化然后重新引入它来解决它,直到我注意到非常明显的异常。
class OtherModel(models.Model):
....
....
....
created_by = models.ForeignKey("MyUser", blank = True, null=True, on_delete = models.CASCADE).
django框架抛出了一个在这种情况下无法识别问题的异常。 我希望它有所帮助。