python - 删除或编辑 admin.TabularInline 中的对象名称

标签 python django django-admin many-to-many

我在管理员中的表格内联看起来像这样:

tabularinline

如何摆脱 DateDeCotisation_adherents 对象 短语?

对于奖励积分,为什么底部有三个空行?

管理员.py

class DatesDeCotisationInline(admin.TabularInline):
    model = DateDeCotisation.adherents.through
    readonly_fields = ['datedecotisation']
    can_delete = False

模型.py

class DateDeCotisation(models.Model):

    date = models.DateTimeField()
    adherents = models.ManyToManyField(Adherent, related_name='adherents_du_jour')

    def __str__(self): return self.date.strftime('%Y-%m-%d')

    class Meta:
        verbose_name = "date de cotisation".encode('utf-8')
        verbose_name_plural = "dates de cotisation".encode('utf-8')
        ordering = ['-date']

最佳答案

该内联应该从模型的内部 Meta 类中获取 verbose_nameverbose_name_plural according to the documentation .然而,我只是将您的代码转储到最新的 django 中,但实际上并没有(或者,也许,它只是在您使用 .through 时才不这样做)。

幸运的是,您始终可以通过直接在 InlineModelAdmin 中设置 verbose_nameverbose_name_plural 来覆盖它,即

class DatesDeCotisationInline(admin.TabularInline):
    model = DateDeCotisation.adherents.through
    readonly_fields = ['datedecotisation']
    can_delete = False
    verbose_name = 'date de cotisation for adherent'
    verbose_name_plural = 'date de cotisation for adherent'

class AdherentAdmin(admin.ModelAdmin):
    inlines = [DatesDeCotisationInline]

admin.site.register(models.Adherent, AdherentAdmin)

(对不起那里的“for”,但我不会说法语)

admin在Django 1.10中出现如下:

django tabular inline


请注意,您永远不需要在 python 3 中执行 .encode('utf-8')。它的字符串默认为 UTF-8。

class Meta:
    verbose_name = "date de cotisation".encode('utf-8')
    verbose_name_plural = "dates de cotisation".encode('utf-8')

(我假设您使用的是 python 3,因为您使用的是 __str__)

关于python - 删除或编辑 admin.TabularInline 中的对象名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37377591/

相关文章:

python - 如何在 rock64 系统上启用/dev/spidev0,0?

python - 根据时间范围重新采样数据帧,忽略日期

python - 在 python 中将 Django 与其他非 Web 系统集成

django - 将管理日期层次结构设置为外键日期字段

python - 编码值形式.ModelForm

python - 通过 smtplib 发送邮件会浪费时间

python - Cartopy 中 pcolormesh 的问题

python - 如何将 django 项目转换为 exe?

python - 在 Django 上使用 SQL 服务器

django - 在 Django Admin 中自定义日期选择器