【问题标题】:Why does Django keeps asking content types are stale and need to be deleted为什么 Django 一直询问内容类型是否过时并且需要删除
【发布时间】:2016-03-04 02:12:54
【问题描述】:

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

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

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

  • 一个ComicBook,它有一个多对多Planche
  • 一个Planche,它有一个多对多Bande
  • 一个Bande,它有一个多对多Vignette
  • ...以及更深的三个层次(这并不重要,它始终是相同的原则)

我需要在多对多表之间添加“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 -> 完美:

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

然后复制/粘贴我的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

我该怎么做才能让他停止询问,问题是什么?

【问题讨论】:

    标签: python django django-models django-modeladmin django-model-utils


    【解决方案1】:

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

    在最后一个示例中发生的情况是,当您第一次创建模型时,django 通过表格创建了自己的标准,然后您通过表格添加了自定义,所以 django 要求您删除原始(旧)的.

    按照您的措辞方式,您似乎将直通表的模型定义放在admin.py 中?为什么要这么做?它们应该在models.py 中,就在它们“连接”的模型旁边。

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

    【讨论】:

    • 我认为你是对的。我发现问题是 (1) 我可以声明我的模型,如 BandeVignetteBandeVignette 而不明确声明 BandeVignette 是代理 (through='BandeVignette') 和然后当我复制/粘贴admin.py 时,django kinddof “发现”我有代理表并尝试创建它们。所以这要么很奇怪,要么是个问题:如果在 models.py 中没有正确声明 admin.py,您应该能够在 admin.py 中声明 Vignette.bulles.through,但您似乎可以这样做那。这导致了问题。
    • 所以我检查你的答案是否有效,因为你提供了足够的建议让新人走上正轨。非常感谢
    猜你喜欢
    • 2016-06-29
    • 2018-02-14
    • 2018-01-11
    • 2012-08-04
    • 2022-07-29
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 2015-06-25
    相关资源
    最近更新 更多