这三行来自何处​​?

时间:2016-02-26 17:26:35

标签: django django-admin

我有三个非常简单的模型:

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)

在管理部分,一切正常:

admin django 1.8

但是当我尝试添加或修改现有的“Conversation”时,我有三行“Messages”无法编辑或删除。这些行来自何处​​?:

three rows coming from hell / admin django 1.8

当我编辑Conversation模型时,同样奇怪的事情:

edit django admin problem

1 个答案:

答案 0 :(得分:1)

内联模型管理员默认包含额外的行,以便于添加新项目。如果您不想要它们,请将extra设置为0。