Skip to main content
 首页 » 编程设计

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

2024年10月24日7youxin

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

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_name 和 verbose_name_plural according to the documentation .然而,我只是将您的代码转储到最新的 django 中,但实际上并没有(或者,也许,它只是在您使用 .through 时才不这样做)。

幸运的是,您始终可以通过直接在 InlineModelAdmin 中设置 verbose_name 和 verbose_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中出现如下:


请注意,您永远不需要在 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__)