Brachamul
Brachamul

Reputation: 1974

Remove or edit object name in admin.TabularInline

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

Upvotes: 5

Views: 3680

Answers (2)

StevenSong
StevenSong

Reputation: 21

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.

Upvotes: 2

grochmal
grochmal

Reputation: 3027

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:

django tabular inline


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

Upvotes: 2

Related Questions