在admin.TabularInline中删除或编辑对象名称 [英] Remove or edit object name in admin.TabularInline

查看:54
本文介绍了在admin.TabularInline中删除或编辑对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何摆脱 DateDeCotisation_adherents对象短语?

要获得奖励积分,为什么在底部有三个空行?

admin.py

  class DatesDeCotisationInline(admin.TabularInline):模型= DateDeCotisation.adherents.throughreadonly_fields = ['datedecotisation']can_delete =假 

models.py

  class DateDeCotisation(models.Model):日期= models.DateTimeField()坚持者=模型.ManyToManyField(坚持者,related_name ='adherents_du_jour')def __str __(self):返回self.date.strftime('%Y-%m-%d')类Meta:verbose_name ="date de cotisation" .encode('utf-8')verbose_name_plural =日期改变".encode('utf-8')排序= ['日期'] 

解决方案

该内联应从内部 Meta 中获取 verbose_name verbose_name_plural 模型的类


请注意,您无需在python 3中执行 .encode('utf-8').默认情况下,其字符串为UTF-8.

  class Meta:verbose_name ="date de cotisation" .encode('utf-8')verbose_name_plural =日期改变".encode('utf-8') 

(由于您使用的是 __ str __ ,所以我认为您使用的是python 3)

My tabular inline in the admin looks like this :

How do I get rid of the DateDeCotisation_adherents object phrase ?

For bonus points, why are there three empty lines at the bottom ?

admin.py

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

models.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']

解决方案

That inline should take the verbose_name and verbose_name_plural from the inner Meta class of the model according to the documentation. Yet, i just dumped your code into the latest django and it actually doesn't (or, maybe, it only doesn't do it when you use .through, nevertheless).

Fortunately, you can always override that by setting verbose_name and verbose_name_plural directly inside the InlineModelAdmin, i.e.

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)

(Sorry for the "for" there but i do not speak French)

The admin appears as follows in Django 1.10:


On a small note you shall never need to do .encode('utf-8') in python 3. Its stings are UTF-8 by default.

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

(I assume you're on python 3 since you're using __str__)

这篇关于在admin.TabularInline中删除或编辑对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆