【发布时间】:2016-06-29 15:23:05
【问题描述】:
我已经阅读了很多有答案的问题,包括 here 和 here,但没有一个有效的答案。这是我的 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_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'),
),
]
我错过了什么?
【问题讨论】:
-
这不是重复的,我已经更新了我的问题
-
我已将我的问题更新为非常精确,以便人们可以找到所有线索来回答。
-
我会回答自己,我找到了问题,我找到了解决方法,但是如果我这样做,它会删除我的代理类,所以我不知道如何解决它以保留我的想要...
标签: django django-models