为什么Django不断要求内容类型陈旧,需要删除 [英] Why does Django keeps asking content types are stale and need to be deleted

查看:219
本文介绍了为什么Django不断要求内容类型陈旧,需要删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过一切:



可以在Django中自动删除陈旧的内容类型?



删除未使用的模型,陈旧的内容类型提示



<一个href =https://stackoverflow.com/questions/30267237/invalidbaseserror-cannot-resolve-bases-for-modelstate-users-groupproxy> InvalidBasesError:无法解析[< ModelState:'users.GroupProxy'> ;]



Django Wagtail CMS迁移:无法解析[< ModelState:'app.CustomPage'> 的基础



Django使用zinnia迁移InvalidBasesError:无法解析[< ModelState:'zinnia.Author'>] 的基础



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




  • a ComicBook 有一个多对多的 Planche

  • a Planche 有一个多对多的 Bande

  • a Bande 有一个多对多的小插曲

  • ...和三个层次更深(这并不重要,它总是相同的原则)



我需要在多对多表之间添加重要性字段,以便能够进行自定义的关系。因此,我创建了




  • a ComicBookPlanche 这是多对多具有字段重要性的表

  • a PlancheBande 许多表字段重要性



一切正常工作,直到我决定重命名 ComicBook to Book 。从现在开始,我总是收到消息 django.db.migrations.state.InvalidBasesError:无法解析...的基础



我甚至尝试删除所有表 迁移文件夹,没有任何改变...我试图评论我的应用程序 - >伟大然后取消评论,然后仍然:

  django.db.migrations.state.InvalidBasesError:
无法解析
[< ModelState:'main.TexteLongTextesThrough'>
< ModelState:'main.TexteCourtTextesThrough'>
< ModelState:'main.VignetteBullesThrough'>
< ModelState:'main.LivrePlanchesThrough'>]

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




  • 全新的应用程序

  • makemigrations 然后迁移 - > auth,admin,会话,网站创建没有问题

  • 复制/粘贴我的 models.py 没有 admin.py



makemigrations - >完美:

 'main'的迁移:
0001_initial.py:
- 创建模型Bande
- 创建模型BandeVignette
- 创建模型Bulle
- 创建模型ContenuCourt
- 创建模型ContenuLong
- 创建模型Langue
创建模型Livre
- 创建模型Personne
- 创建模型Planche
- 创建模型PlancheBande
- 创建模型TexteCourt
- 创建模型TexteLong
- 创建模型Vignette
- 将字段描述添加到planche
- 添加字段平面到livre

然后 mig价格 - >完美:

 要执行的操作:
同步未迁移的应用程序:staticfiles,消息
应用所有迁移:会话,管理员,站点,auth,contenttypes,main
同步不需要迁移的应用程序:
创建表...
运行延迟SQL ...
安装自定义SQL ...
运行迁移:
呈现模型状态... DONE
应用main.0001_initial ... OK

处理完成退出代码0

然后复制/粘贴我的 admin.py 然后 makemigrations - >完美:

 
0002_livreplanchesthrough_textecourttextesthrough_textelongtextesthrough_vignettebullesthrough.py:
- 创建代理模型LivrePlanchesThrough
- 创建代理模型TexteCourtTextesThrough
- 创建代理模型TexteLongTextesThrough
- 创建代理模型VignetteBullesThrough

Proce ss完成退出代码0

然后每次尝试迁移它不断问我这个,不管我是否是或否:

 >> ;>迁移
要执行的操作:
同步未迁移的应用程序:staticfiles,消息
应用所有迁移:会话,管理员,站点,auth,contenttypes,main
同步不迁移的应用程序:
创建表...
运行延迟SQL ...
安装自定义SQL ...
运行迁移:
无需迁移。
以下内容类型陈旧,需要删除:

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

通过外键与这些内容类型相关的任何对象也将删除
。您确定要删除这些内容类型吗?
如果您不确定,请回答否。

输入是继续,或否取消:是
处理完成退出代码0

我可以做些什么来让他停下来问问什么问题?

解决方案

这里有几件事:看起来您在一批迁移中创建了模型,然后在第二批迁移中创建了通过表。这是错误的,你应该在主模型的同一时间写入和迁移通过表。最后一个例子中发生的事情是,当你第一次创建模型时,django去创建它自己的标准通过表,然后你去通过表添加自定义,所以django要求您删除原始(旧的)。



您对所有内容的措辞,您似乎将 code> admin.py ?为什么要这么做?他们应该在 models.py 旁边的模型,他们是连接。



另外,你应该不要使用代理模型,没有实际的源代码,这可能是您问题的根本原因。如果您想要做的只是在通过关系中增加一个字段,那么您应该按照以下格式: https://docs.djangoproject.com/en/1.8/topics/db/models/#extra-fields-on-many-多对多关系


I've tried everything found:

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

So here's my problem: I have:

  • a ComicBook that has a many-to-many Planche's
  • a Planche that has a many-to-many Bande's
  • a Bande that has a many-to-many Vignette's
  • ... and three levels deeper (it's not important it's always the same principle)

I needed in-between many-to-many tables to add "importance" field to be able to make a custom sort of the relationships. Thus I've created

  • a ComicBookPlanche that is the many-to-many table with field importance
  • a PlancheBande that is the many-to-many table with field importance

Everything was working perfectly until I decide to rename ComicBook to Book. From now on I always get the message django.db.migrations.state.InvalidBasesError: Cannot resolve bases for...

I even tried to drop all the tables and migration folder, nothing changed... I tried to comment my application -> great then un-comment and still:

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

I'm getting mad. So here's what I did:

  • brand new application
  • makemigrations then migrate -> auth, admin, sessions, sites created no problem
  • copy/paste my models.py without admin.py.

makemigrations -> perfect:

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

Then migrate -> perfect:

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

Then copy/paste my admin.py then makemigrations -> perfect:

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

Then each time I try migrate it keeps asking me this, no matter if I anwser "yes" or "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

What can I do to make him stop asking, and what is the problem?

解决方案

A few things here: It looks like you created the models in one batch of migrations, and then created the through tables in a second batch of migrations. This is wrong, you should have the through tables written and migrated at the same time of the main models.

What happened in the last example is that when you first created the models, django went and created it's own standard through tables, then you went and added custom through tables, so django is asking you to delete the original (old) ones.

The way you've worded everything, it looks like you placed the model definitions for the through tables in admin.py? Why would you do that? They should be in models.py right next to the models they are "connecting".

Also, you shouldn't be using a Proxy model, and without the actual source code, that may very well be the root cause of your problem. If all you're trying to do is have an extra field on the through relationship, you should follow the pattern here: https://docs.djangoproject.com/en/1.8/topics/db/models/#extra-fields-on-many-to-many-relationships

这篇关于为什么Django不断要求内容类型陈旧,需要删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆