在 django admin 中获取对象名称

皮亚

嘿!

我想在我的管理区域中有一个营业额类别,在那里我有一个货币字段。这是一个带有预先给定货币的下拉字段。

我的问题是,没有显示实际货币,而是显示它们的 ID。(如对象 123)。

有谁知道如何为内联解决这个问题?

# models.py

class Currency(models.Model):
    code = models.CharField(max_length=3, unique=True)
    currency = models.CharField(max_length=150, unique=True)


class Turnover(models.Model):
    currency = models.ForeignKey(Currency, on_delete=models.PROTECT)

# admin.py

class TurnoverInline(admin.TabularInline):
    model = Turnover
    extra = 1
    classes = ["collapse"]


class SomeAdmin(admin.ModelAdmin):
    form = SomeForm
    save_on_top = True

    inlines = [
        TurnoverInline
    ]

对于类似的问题,我创建了 admin.ModelAdmins 并在模型中集成了 code_str(self) 和str (self) 方法。但那些不是内联的,并且在 SomeForm/SomeAdmin 中作为 fields/autocomplete_fields。

布赖恩·D

您可以__str__Currency模型中使用

class Currency(models.Model):
    code = models.CharField(max_length=3, unique=True)
    currency = models.CharField(max_length=150, unique=True)
    
    def __str__(self):
        return f'{self.code} - {self.currency}'

下拉菜单应显示此格式而不是对象和 ID。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章