【问题标题】:Always asking: "The following content types are stale and need to be deleted"!总是问:“以下内容类型已过时,需要删除”!
【发布时间】:2016-06-29 15:23:05
【问题描述】:

我已经阅读了很多有答案的问题,包括 herehere,但没有一个有效的答案。这是我的 2 个有问题的模型:

# coding=UTF-8

from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _

from app.models.generic import BaseModel
from app.models.personne import Personne


@python_2_unicode_compatible
class Message(BaseModel):
    src = models.ForeignKey('Personne', related_name='message_src')
    dst = models.ForeignKey('Personne', related_name='message_dst')
    is_read = models.BooleanField(default=False)
    message = models.TextField(null=True, blank=True,
                               verbose_name=_(u'Messages'))
    def __str__(self):
        return u'{} : {} <> {} ({}) : "{}"'.format(
            self.date_creation.strftime('%Y-%m-%d %H:%M:%S'),
            self.src.full_name(), self.dst.full_name(),
            _(u'read') if self.is_read else _(u'unread'),
            self.message_summary()
        ).strip()

    class Meta:
        ordering = ["date_creation"]


@python_2_unicode_compatible
class Conversation(BaseModel):

    personnes = models.ManyToManyField(Personne, related_name='conversations')
    messages = models.ManyToManyField(Message, related_name='conversations')

    order_with_respect_to = 'messages'

    def __str__(self):
        return _(u'Conversation n.{}').format(self.pk).strip()

每次我做migrate时,我都会收到这个问题,我总是回答yes

The following content types are stale and need to be deleted:

    app | conversation_personnes
    app | conversation_messages

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

这不可能与this question 重复。

这是我的django_content_type 表的快照,有没有模型像conversation_personnesconversation_messages

因此,如果我尝试:

>>> from django.contrib.contenttypes.models import ContentType
ct = ContentType.objects.get(app_label='app', model='conversation_personnes')
ct.delete()

Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 334, in get
    self.model._meta.object_name
DoesNotExist: ContentType matching query does not exist.

我在整个代码中找到conversation_personnes 的唯一位置是一个迁移文件,其中包含:

File 0060_messagethrough.py

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0059_auto_20160226_1752'),
    ]

    operations = [
        migrations.CreateModel(
            name='MessageThrough',
            fields=[
            ],
            options={
                'proxy': True,
            },
            bases=('app.conversation_messages',),
        ),
    ]

...和

File 0058_auto_20160225_0106.py

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0057_conversation_personnes'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='message',
            name='conversation',
        ),
        migrations.AddField(
            model_name='conversation',
            name='messages',
            field=models.ManyToManyField(to='app.Message'),
        ),
    ]

我错过了什么?

【问题讨论】:

  • 这不是重复的,我已经更新了我的问题
  • 我已将我的问题更新为非常精确,以便人们可以找到所有线索来回答。
  • 我会回答自己,我找到了问题,我找到了解决方法,但是如果我这样做,它会删除我的代理类,所以我不知道如何解决它以保留我的想要...

标签: django django-models


【解决方案1】:

问题来自我的admin.py,我在其中创建了一个代理类:这两个代理在这里,所以我可以在管理面板中更改标签。所以这是有效的最终代码=回答我的问题,但我必须删除我的代理类,所以我无法更改管理员中的标签......如果有人有一个可行的想法,我是你的人!

# class MessageThrough(Conversation.messages.through):
#     class Meta:
#         proxy = True
#
#     def __unicode__(self):
#         return str(self.message)


class ConversationMessagesInline(CollapsedStackedInline):
    # model = MessageThrough
    model = Conversation.messages.through
    fields = ('message',)
    raw_id_fields = ('message',)
    extra = 0

    verbose_name = u"Message"
    verbose_name_plural = u"Messages"


# class PersonneThrough(Conversation.personnes.through):
#     class Meta:
#         proxy = True
#
#     def __unicode__(self):
#         return self.personne.full_name()


class ConversationPersonnesInline(CollapsedStackedInline):
    # model = PersonneThrough
    model = Conversation.personnes.through
    fields = ('personne',)
    raw_id_fields = ('personne',)
    extra = 0

    verbose_name = u"Personne"
    verbose_name_plural = u"Personnes"

【讨论】:

    【解决方案2】:

    回答有点晚了,但我刚刚发现了这个,对于遇到同样问题的人。

    我找到了this,解决方案 1 对我有用:

    class MyInline(admin.TabularInline):
        MyModel.m2m.through.__unicode__ = lambda x: 'My New Str'
        model = MyModel.m2m.through
    

    如果您使用的是 Python 3+,请记住将 'unicode' 更改为 'str'。

    【讨论】:

      猜你喜欢
      • 2016-03-04
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      相关资源
      最近更新 更多