django 1.8.2:创建新内容类型时出错

django 1.8.2: Error creating new content types

我正在使用 django 1.8.2,突然我不能再使用 migrate

我搜索了很多,发现 建议我应该从 django_content_type table 中删除 name。但该列不在 django_content_type table.

这是我的 django_content_type table:

 id |     app_label     |     model      
----+-------------------+----------------
  1 | admin             | logentry
  2 | auth              | permission
  3 | auth              | group
  4 | auth              | user
  5 | contenttypes      | contenttype
  6 | sessions          | session
  7 | centuryPhotograph | temporaryuser
  8 | centuryPhotograph | userinfo
  9 | centuryPhotograph | log
 10 | centuryPhotograph | uploadedimages
(10 rows)

这里是完整的错误:

System check identified some issues:

WARNINGS:
centuryPhotograph.Galleries.closed: (1_6.W002) BooleanField does not have a default value.
    HINT: Django 1.6 changed the default value of BooleanField from False to None. See https://docs.djangoproject.com/en/1.6/ref/models/fields/#booleanfield for more information.
centuryPhotograph.Galleries.open_gallery: (1_6.W002) BooleanField does not have a default value.
    HINT: Django 1.6 changed the default value of BooleanField from False to None. See https://docs.djangoproject.com/en/1.6/ref/models/fields/#booleanfield for more information.
Operations to perform:
  Apply all migrations: admin, centuryPhotograph, contenttypes, auth, sessions
Running migrations:
  No migrations to apply.
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 165, in handle
    emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/sql.py", line 268, in emit_post_migrate_signal
    using=db)
  File "/usr/local/lib/python2.7/dist-packages/django/dispatch/dispatcher.py", line 198, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/management/__init__.py", line 83, in create_permissions
    ctype = ContentType.objects.db_manager(using).get_for_model(klass)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/contenttypes/models.py", line 58, in get_for_model
    " is migrated before trying to migrate apps individually."
RuntimeError: Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.

这是我的 installed_apps:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'centuryPhotograph',
)

我删除了一次所有迁移并尝试迁移,但问题仍然存在。

我在 Django 1.9 上,遇到了上面评论中发布的相同 problem.The 解决方案对我不起作用,因此发布对我有用的解决方案:

  1. 从所有应用程序中删除所有迁移文件夹。
  2. 运行 python manage.py migrate
  3. 运行 python manage.py makemigrations
  4. 运行 python manage.py migrate

这里的关键是运行migrate一次之前你运行makemigrations.

这将 运行 在 Django 的所有默认模型上迁移,例如:contenttypes, admin, sites, auth, sessions

完成此操作后,您 运行 makemigrations 将为您的项目自定义模型进行迁移。

最后,当您 运行 再次迁移时,它会迁移您所有的自定义模型,并且不会在 contenttype 上抱怨没有迁移 运行。