我已经阅读了许多问题,其中包括here和here,但没有答案。以下是我的两个有问题的模型:
# 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_personnes
或conversation_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'),
),
]
我错过了什么?
答案 0 :(得分:0)
问题来自我创建代理类的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"
答案 1 :(得分:0)
回答有点晚,但我发现了这一点,因为人们遇到了同样的问题。
我发现this,解决方案编号为1,对我有用:
class MyInline(admin.TabularInline):
MyModel.m2m.through.__unicode__ = lambda x: 'My New Str'
model = MyModel.m2m.through
如果您使用的是Python 3 +,请务必将“ unicode ”更改为“ str ”。