Remove or edit object name in admin.TabularInline

狂风中的少年 提交于 2021-02-10 06:47:20

问题


My tabular inline in the admin looks like this :

tabularinline

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

回答1:


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__)




回答2:


In Django 3, the title of each object for inline admin (DateDeCotisation_adherents object in the original post) comes from the model's __str__ method. Also the extra lines are likely because of the extra attribute of the inline admin class. The default set in admin.InlineModelAdmin (parent of Stacked and Tabular inline) has extra = 3. In your inline admin, set extra = 0. Feel free to take a look at all the options in django.contrib.admin.options.py.



来源:https://stackoverflow.com/questions/37377591/remove-or-edit-object-name-in-admin-tabularinline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!