我有三个非常简单的模型:
class BaseModel(models.Model):
date_creation = models.DateTimeField(auto_now_add=True)
class Personne(BaseModel):
user = models.OneToOneField(User)
class Message(BaseModel):
src = models.ForeignKey('Personne', related_name='message_src')
dst = models.ForeignKey('Personne', related_name='message_dst')
message = models.TextField(null=True, blank=True,
verbose_name=_(u'Messages'))
def message_summary(self):
a = self.message
if a:
return (a[:85] + '...') if len(a) > 90 else a
return u''
def __str__(self):
return _(u'{} : {} <> {} : "{}"').format(
self.date_creation.strftime('%Y-%m-%d %H:%M:%S'),
self.src.full_name(), self.dst.full_name(), self.message_summary()
)
class Conversation(BaseModel):
personnes = models.ManyToManyField(Personne)
messages = models.ManyToManyField(Message)
def __str__(self):
return _(u'Conversation n.{}').format(self.pk)
现在,在管理部分我声明如下:
class MessageThrough(Conversation.messages.through):
class Meta:
proxy = True
def __unicode__(self):
return str(self.message)
class ConversationMessagesInline(CollapsedStackedInline):
model = MessageThrough
fields = ('message',)
raw_id_fields = ('message',)
verbose_name = u"Message"
verbose_name_plural = u"Messages"
class ConversationAdmin(admin.ModelAdmin):
fieldsets = (
(None, {
'fields': (('personnes',),)
}),
(_(u'Administration only'), {
'classes': ('collapse',),
'fields': ('date_v_debut', 'date_v_fin')
}),
(_(u'Administration only'), {
'classes': ('collapse',),
'fields': ()
}),
)
inlines = (ConversationMessagesInline,)
admin.site.register(Conversation, ConversationAdmin)
admin.site.register(Message)
在管理部分,一切正常:
Conversation
”时,我有三行“Messages
”无法编辑或删除。这些行来自何处?:Conversation
模型时,同样奇怪的事情: