【问题标题】:How to make Django admin shows OneToMany instead of ForeignKey如何让 Django 管理员显示 OneToMany 而不是 ForeignKey
【发布时间】:2015-04-14 22:38:05
【问题描述】:

在 Django 管理页面中,当我们在对象中使用 ForeignKey 时,管理员会显示在具有 ForeignKey 值的模型中设置 ForeignObject 的选项。

例如:

class Diferencial(SobreFather):
    class Meta:
        verbose_name = 'Diferencial'
        verbose_name_plural = 'Diferenciais'

class DiferencialItem(models.Model):
    diferencial = models.ForeignKey(Diferencial)
    icone = models.ImageField(upload_to="icones_diferencial")
    texto = models.CharField(max_length=50, null=False, blank=False)

    def __unicode__(self):
        return self.diferencial

    class Meta:
        verbose_name = 'Item Diferencial'
        verbose_name_plural = 'Itens Diferencial'

此代码将显示在 django admin 中选择要与 DiferencialItem 相关的 Diferencial 对象的选项。

有没有办法在差异管理视图中显示创建差异项目的选项?

【问题讨论】:

标签: python django django-models


【解决方案1】:

是的,使用inline model admin

class DiferencialItemInline(admin.TabularInline):
    model = DiferencialItem

class DiferencialAdmin(admin.ModelAdmin):
    inlines = [DiferencialItemInline]

【讨论】:

    猜你喜欢
    • 2010-12-08
    • 2014-09-08
    • 2016-06-20
    • 2019-05-10
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    相关资源
    最近更新 更多