我正在开发带有跨站点引用的Django(1.9)Rest后端和AngularJS前端。尝试执行./manage.py dumpdata命令时,它将引发以下异常:

$ python manage.py dumpdata -o dev/dumpdata.json
CommandError: Unable to serialize database: relation
"corsheaders_corsmodel" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "corsheaders_corsmodel"

知道如何处理吗?

最佳答案

我遇到了同样的问题,并通过专门针对python manage.py makemigrations应用程序调用corsheaders来解决了该问题:

$ python manage.py makemigrations corsheaders
$ python manage.py migrate

我认为发生的情况是,从Django 1.8升级到1.9后,在更新数据库时从未应用过初始迁移。

我通过注意到corsheaders应用未在Apply all migrationspython manage.py migrate输出中列出来进行跟踪:
$ python manage.py migrate
Operations to perform:
  Apply all migrations: sessions, admin, xyz, auth, contenttypes
Running migrations:
  No migrations to apply.

但是,为corsheaders运行手动迁移实际上会创建初始迁移:
$ python manage.py makemigrations corsheaders
Migrations for 'corsheaders':
  0001_initial.py:
    - Create model CorsModel

完成此操作后,migrate的确在输出中显示corsheaders,并按预期成功应用了迁移:
$ python manage.py migrate
Operations to perform:
  Apply all migrations: corsheaders, sessions, admin, xyz, auth, contenttypes
Running migrations:
  Rendering model states... DONE
  Applying corsheaders.0001_initial... OK

10-06 14:44