为什么Django总是问内容类型是过时的,需要删除

2024-09-24 04:26:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试过所有发现的东西:

Can stale content types be automatically deleted in Django?

Deleting unused Models, stale content types prompt

InvalidBasesError: Cannot resolve bases for [<ModelState: 'users.GroupProxy'>]

Django Wagtail CMS migrate: Cannot resolve bases for [<ModelState: 'app.CustomPage'>

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

所以我的问题是:我有:

  • 一个具有多对多PlancheComicBook
  • 一个具有多对多BandePlanche
  • 一个具有多对多VignetteBande
  • 。。。更深层的三个层次(这并不重要,原则总是一样的)

我需要在多对多的表之间添加“importance”字段,以便能够定制某种关系。所以我创造了

  • 一个ComicBookPlanche,它是带有字段importance的多对多表
  • 一个PlancheBande,它是带有字段importance的多对多表

在我决定将ComicBook重命名为Book之前,一切都很顺利。从现在起,我总是收到消息django.db.migrations.state.InvalidBasesError: Cannot resolve bases for...

我甚至试图删除所有的表和迁移文件夹。。。我试图对我的申请进行评论->很好然后取消评论,但仍然:

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

我快疯了。所以我是这么做的:

  • 全新应用
  • makemigrations然后migrate->;验证、管理、会话、站点创建没有问题
  • 复制/粘贴我的models.py而不使用admin.py。在

makemigrations->;完美:

^{pr2}$

然后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

然后复制/粘贴我的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它总是问我这个问题,不管我回答“是”还是“否”:

>>> 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

我该怎么做才能让他不再问,问题出在哪里?在


Tags: togtformodelmainmigrationswithmigrate
1条回答
网友
1楼 · 发布于 2024-09-24 04:26:07

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

在最后一个例子中发生的事情是,当您第一次创建模型时,django创建了自己的标准穿透表,然后您添加了custom through表,所以django要求您删除原始的(旧的)表。在

按照您编写所有内容的方式,您似乎将through表的模型定义放在admin.py中?你为什么要那样做?它们应该在models.py中,就在它们“连接”的模型旁边。在

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

相关问题 更多 >