为什么 Django 不断询问内容类型是否过时需要删除

Why does Django keeps asking content types are stale and need to be deleted

我已经尝试了所有找到的方法:

Can stale content types be automatically deleted in Django?

Django migrate with zinnia- InvalidBasesError: Cannot resolve bases for [<ModelState: 'zinnia.Author'>]

所以这是我的问题:我有:

我需要在多对多 table 之间添加“importance”字段,以便能够对关系进行自定义排序。因此我创造了

一切正常,直到我决定将 ComicBook 重命名为 Book。从现在开始,我总是收到消息 django.db.migrations.state.InvalidBasesError: Cannot resolve bases for...

我什至试图删除所有table迁移文件夹, 什么都没有改变...我尝试评论我的应用程序 -> 很棒然后取消评论并且仍然:

django.db.migrations.state.InvalidBasesError:
Cannot resolve bases for
[<ModelState: 'main.TexteLongTextesThrough'>,
 <ModelState: 'main.TexteCourtTextesThrough'>,
 <ModelState: 'main.VignetteBullesThrough'>,
 <ModelState: 'main.LivrePlanchesThrough'>]

我快生气了。所以这就是我所做的:

makemigrations -> 完美:

Migrations for 'main':
  0001_initial.py:
    - Create model Bande
    - Create model BandeVignette
    - Create model Bulle
    - Create model ContenuCourt
    - Create model ContenuLong
    - Create model Langue
    - Create model Livre
    - Create model Personne
    - Create model Planche
    - Create model PlancheBande
    - Create model TexteCourt
    - Create model TexteLong
    - Create model Vignette
    - Add field description to planche
    - Add field planches to livre

然后migrate -> 完美:

Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: sessions, admin, sites, auth, contenttypes, main
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying main.0001_initial... OK

Process finished with exit code 0

然后 copy/paste 我的 admin.py 然后 makemigrations -> 完美:

Migrations for 'main':
  0002_livreplanchesthrough_textecourttextesthrough_textelongtextesthrough_vignettebullesthrough.py:
    - Create proxy model LivrePlanchesThrough
    - Create proxy model TexteCourtTextesThrough
    - Create proxy model TexteLongTextesThrough
    - Create proxy model VignetteBullesThrough

Process finished with exit code 0

然后每次我尝试 migrate 它都会一直问我这个,不管我是 "yes" 还是 "no":

>>> migrate
Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: sessions, admin, sites, auth, contenttypes, main
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  No migrations to apply.
The following content types are stale and need to be deleted:

    main | textelong_textes
    main | textecourt_textes
    main | livre_planches
    main | vignette_bulles

Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.

    Type 'yes' to continue, or 'no' to cancel:  yes
Process finished with exit code 0

我该怎么做才能让他不再问,问题是什么?

这里有几件事:看起来您在一批迁移中创建了模型,然后在第二批迁移中创建了直通表。这是错误的,您应该在主要模型的同时编写和迁移直通表。

最后一个例子发生的事情是,当你第一次创建模型时,django 通过表创建了它自己的标准,然后你通过表添加了自定义,所以 django 要求你删除原来的(旧的) ) 个。

按照您对所有内容的措辞方式,您似乎将直通表的模型定义放在了 admin.py 中?为什么要这么做?它们应该在 models.py 中,就在它们 "connecting" 的模型旁边。

此外,您不应该使用 Proxy 模型,没有实际的源代码,这很可能是您问题的根本原因。如果您想要做的只是在直通关系上有一个额外的字段,您应该遵循此处的模式:https://docs.djangoproject.com/en/1.8/topics/db/models/#extra-fields-on-many-to-many-relationships