How can I change the TabularInline "title" on Django Admin?

4,910 views
Skip to first unread message

Fellipe Henrique

unread,
Jul 7, 2016, 12:57:18 PM7/7/16
to Django Users
Hello,

When we need to use TabulerInline on Django Admin, the admin template create a "title" for the field... it's not the verbose_name... verbose_name is set like a Group.. "Blue" line before TabularInline... after this line, we have a table title... that's my problem... how can I change these "table title" on TabularInline?

Cheers

T.·.F.·.A.·.     S+F
Fellipe Henrique P. Soares

e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
Twitter: @fh_bash

Michal Petrucha

unread,
Jul 7, 2016, 1:58:26 PM7/7/16
to django...@googlegroups.com
On Thu, Jul 07, 2016 at 01:56:08PM -0300, Fellipe Henrique wrote:
> Hello,
>
> When we need to use TabulerInline on Django Admin, the admin template
> create a "title" for the field... it's not the verbose_name... verbose_name
> is set like a Group.. "Blue" line before TabularInline... after this line,
> we have a table title... that's my problem... how can I change these "table
> title" on TabularInline?

Maybe you're referring to verbose_name_plural?

Cheers,

Michal
signature.asc

Fellipe Henrique

unread,
Jul 7, 2016, 2:59:58 PM7/7/16
to Django Users

On Thu, Jul 7, 2016 at 2:57 PM, Michal Petrucha <michal....@koniiiik.org> wrote:
Maybe you're referring to verbose_name_plural?

Unfortunately no.. :(

Here is a example: (in pt-br sorry...)
Inline image 2

I want to change the "DATASET" and "Dataset_users object"

verbose_name and verbose_name_plural change the text on the "blue" line...

Michal Petrucha

unread,
Jul 7, 2016, 4:25:08 PM7/7/16
to django...@googlegroups.com
On Thu, Jul 07, 2016 at 03:29:00PM -0300, Fellipe Henrique wrote:
> On Thu, Jul 7, 2016 at 2:57 PM, Michal Petrucha <
> michal....@koniiiik.org> wrote:
>
> > Maybe you're referring to verbose_name_plural?
> >
>
> Unfortunately no.. :(
>
> Here is a example: (in pt-br sorry...)
> [image: Inline image 2]
>
> I want to change the "DATASET" and "Dataset_users object"
>
> verbose_name and verbose_name_plural change the text on the "blue" line...

In that case, the “DATASET” would be the verbose_name of that field,
and “Dataset_users object” is the string representation of the
original original object that the row corresponds to.

Cheers,

Michal
signature.asc

Mike Dewhirst

unread,
Jul 7, 2016, 6:57:52 PM7/7/16
to django...@googlegroups.com
On 8/07/2016 2:56 AM, Fellipe Henrique wrote:
> Hello,
>
> When we need to use TabulerInline on Django Admin, the admin template
> create a "title" for the field... it's not the verbose_name...
> verbose_name is set like a Group.. "Blue" line before TabularInline...
> after this line, we have a table title... that's my problem... how can I
> change these "table title" on TabularInline?


fieldsets = (
('This ought to change the title', {
'classes': ('collapse', 'wide'),
'fields': (
'foo',
'bar',
'baz',
),
}
),
)




>
> Cheers
>
> T.·.F.·.A.·.   S+F
> *Fellipe Henrique P. Soares*
>
> e-mail: > echo "lkrrovknFmsgor4ius" | perl -pe \ 's/(.)/chr(ord($1)-2*3)/ge'
> /Fedora Ambassador:Â https://fedoraproject.org/wiki/User:Fellipeh/
> /Blog:Â /http:www.fellipeh.eti.br
> /GitHub:Â https://github.com/fellipeh/
> /Twitter: @fh_bash/
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAF1jwZHe-w%3DQf%2B%3DqwofNnEd1%2B_U_6P%2Bj%2BDBqN%2BjQHyhmkayd%3DQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAF1jwZHe-w%3DQf%2B%3DqwofNnEd1%2B_U_6P%2Bj%2BDBqN%2BjQHyhmkayd%3DQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Fellipe Henrique

unread,
Jul 12, 2016, 11:56:29 AM7/12/16
to Django Users

On Thu, Jul 7, 2016 at 5:24 PM, Michal Petrucha <michal....@koniiiik.org> wrote:
In that case, the “DATASET” would be the verbose_name of that field,
and “Dataset_users object” is the string representation of the
original original object that the row corresponds to.

Unfortunately no.. :(

I have set all verbose_name and verbose_name_plural, on all models.. and all models have str representation.. but still show these dataset_users object... instead the verbose_name

These "problem" occur only when I have M2M field.. in other FK fields doesn't appears..

Ramiro Morales

unread,
Jul 12, 2016, 12:35:51 PM7/12/16
to django...@googlegroups.com
On Tue, Jul 12, 2016 at 12:54 PM, Fellipe Henrique <fell...@gmail.com> wrote:

On Thu, Jul 7, 2016 at 5:24 PM, Michal Petrucha <michal....@koniiiik.org> wrote:
In that case, the “DATASET” would be the verbose_name of that field,
and “Dataset_users object” is the string representation of the
original original object that the row corresponds to.

Unfortunately no.. :(

I have set all verbose_name and verbose_name_plural, on all models.. and all models have str representation.. but still show these dataset_users object... instead the verbose_name

These "problem" occur only when I have M2M field.. in other FK fields doesn't appears..


Can you post a simplified excerpt of the relevant (fields, Meta options, __*__ methods) models, admin app ModelAdmin's and *Inline's involved which result in the layout depicted in the screenshot you posted?

Regards,

--
Ramiro Morales
@ramiromorales

Fellipe Henrique

unread,
Jul 12, 2016, 12:45:43 PM7/12/16
to Django Users

On Tue, Jul 12, 2016 at 1:34 PM, Ramiro Morales <cra...@gmail.com> wrote:
Can you post a simplified excerpt of the relevant (fields, Meta options, __*__ methods) models, admin app ModelAdmin's and *Inline's involved which result in the layout depicted in the screenshot you posted?

Sure, here is:

class Dataset(models.Model):
active = models.BooleanField(_('Ativo'), default=True)
title = models.CharField(_('Nome'), max_length=250)
users = models.ManyToManyField(MyCustomUser)

class Meta:
verbose_name = _('Dataset')
verbose_name_plural = _('Datasets')

def __str__(self):
return self.title

class MyCustomUser(AbstractBaseUser, PermissionsMixin):
username = models.CharField(
_('Nome do usuário'), max_length=200, blank=True, null=True)
email = models.EmailField(
_('Endereço de E-Mail'), blank=False, unique=True)
first_name = models.CharField(_('Nome'), max_length=30, blank=True)
last_name = models.CharField(
_('Sobrenome'), max_length=30, blank=True, default='')
is_staff = models.BooleanField(_('Administrador?'), default=False)
is_active = models.BooleanField(_('Usuário Ativo?'), default=True)
date_joined = models.DateTimeField(
_('Data Cadastro'), default=timezone.now)

objects = CustomUserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name']

class Meta:
verbose_name = _('Usuário')
verbose_name_plural = _('Usuários')

def __str__(self):
if self.first_name or self.last_name:
return self.get_full_name()
return self.email

Admin:

class DatasetInline(admin.TabularInline):
model = Dataset.users.through
verbose_name = _('Dataset')
verbose_name_plural = _('Datasets')


class MyCustomUserAdmin(UserAdmin):
model = User
add_fieldsets = (None, {
'classes': ('wide',),
'fields': (
'email', 'password1', 'password2', 'first_name', 'last_name',
'is_superuser'),
}),

fieldsets = (
(None, {'fields': ('email', 'password')}),
(_('Personal info'), {'fields': (
'first_name', 'last_name')}),
(_('Permissions'), {'fields': ('is_active', 'is_superuser')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)

ordering = ('first_name', 'email')
list_display = ('email', 'first_name', 'last_name')
inlines = [DatasetInline]
list_filter = ('is_active', 'is_superuser')


admin.site.register(User, MyCustomUserAdmin)

The results:

Inline image 1

Cheers!

Ramiro Morales

unread,
Jul 12, 2016, 12:52:58 PM7/12/16
to django...@googlegroups.com
On Tue, Jul 12, 2016 at 1:44 PM, Fellipe Henrique <fell...@gmail.com> wrote:

On Tue, Jul 12, 2016 at 1:34 PM, Ramiro Morales <cra...@gmail.com> wrote:
Can you post a simplified excerpt of the relevant (fields, Meta options, __*__ methods) models, admin app ModelAdmin's and *Inline's involved which result in the layout depicted in the screenshot you posted?

Sure, here is:
[...]


Where the 'Conjunto de dados' seen in the screenshot title comes from? Is the code you posted actually in sync with it?

--
Ramiro Morales
@ramiromorales

Fellipe Henrique

unread,
Jul 12, 2016, 1:44:07 PM7/12/16
to Django Users

On Tue, Jul 12, 2016 at 1:52 PM, Ramiro Morales <cra...@gmail.com> wrote:
Where the 'Conjunto de dados' seen in the screenshot title comes from? Is the code you posted actually in sync with it?

Sorry about that, I take the screenshot before change these text to "DataSet"...

Inline image 1

My problem is, the second "Dataset" (on light gray line) and "Dataset_users object"

Lachlan Musicman

unread,
Jul 12, 2016, 7:40:11 PM7/12/16
to django...@googlegroups.com
There's a ticket for this in the tracker iirc, but I can't find it now. I think the reason is because that's the name of the DB field - it's not localizable (in so much as we don't localize db field names).

I *think* that was the reason.

------
The most dangerous phrase in the language is, "We've always done it this way."

- Grace Hopper

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Ramiro Morales

unread,
Jul 13, 2016, 6:51:46 AM7/13/16
to django...@googlegroups.com
On Tue, Jul 12, 2016 at 8:39 PM, Lachlan Musicman <dat...@gmail.com> wrote:
There's a ticket for this in the tracker iirc, but I can't find it now. I think the reason is because that's the name of the DB field - it's not localizable (in so much as we don't localize db field names).

That was mi recollection too. But I've been unable to find it.

Ramiro Morales

unread,
Jul 13, 2016, 7:28:52 AM7/13/16
to django...@googlegroups.com
On Tue, Jul 12, 2016 at 2:42 PM, Fellipe Henrique <fell...@gmail.com> wrote:


On Tue, Jul 12, 2016 at 1:52 PM, Ramiro Morales <cra...@gmail.com> wrote:
Where the 'Conjunto de dados' seen in the screenshot title comes from? Is the code you posted actually in sync with it?

Sorry about that, I take the screenshot before change these text to "DataSet"...

Inline image 1

My problem is, the second "Dataset" (on light gray line) and "Dataset_users object"


I've prepared this simplified app.

Note: All the is completely centered around controlling the labels in the admin views, this can mean things get completely ugly on ther UI areas.

The Dataset model setup is basically what you posted originally, the Sample model is very similar to Dataset but the M2M relationship between User and Sample is defined through an explicit SampleUserRel model. See

  https://docs.djangoproject.com/en/1.9/topics/db/models/#intermediary-manytomany

  https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.ManyToManyField.through

sections of the Django documentation.

#--------------------------------------------
# models.py
from __future__ import unicode_literals

from django.db import models
from django.utils.encoding import python_2_unicode_compatible


@python_2_unicode_compatible
class User(models.Model):
    username = models.CharField(max_length=200, blank=True, null=True)
    email = models.EmailField(blank=False, unique=True)

    def __str__(self):
        return self.email


@python_2_unicode_compatible
class Dataset(models.Model):
    active = models.BooleanField(default=True)
    title = models.CharField(max_length=250)
    users = models.ManyToManyField(User)

    def __str__(self):
        return self.title


@python_2_unicode_compatible
class Sample(models.Model):
    active = models.BooleanField(default=True)
    title = models.CharField(max_length=250)
    users = models.ManyToManyField(User, through='SampleUserRel')

    def __str__(self):
        return self.title


@python_2_unicode_compatible
class SampleUserRel(models.Model):
    user = models.ForeignKey(User)
    sample = models.ForeignKey(Sample, verbose_name='this can be set with the verbose_name option of the FK field to the remote model in the intermediate model')

    def __str__(self):
        return "this can be set with the intermediate model's __str__ method"


#--------------------------------------------
# admin.py
from django.contrib import admin

from .models import Dataset, User, Sample, SampleUserRel



class DatasetInline(admin.TabularInline):
    model = Dataset.users.through
    verbose_name = 'this can be set with {Stacked,Tabular}Inline.verbose_name'
    verbose_name_plural = 'this can be set with {Stacked,Tabular}.verbose_name_plural'


class SampleUserRelInline(admin.TabularInline):
    model = SampleUserRel
    extra = 1
    verbose_name = "this can be set with intermediate's model verbose_name and overridden with {Stacked,Tabular}Inline.verbose_name"
    verbose_name_plural = "this can be set with intermediate model's verbose_name_plural and overridden with {Stacked,Tabular}Inline.verbose_name_plural"


class UserAdmin(admin.ModelAdmin):
    list_display = ('email',)
    inlines = [DatasetInline, SampleUserRelInline]


admin.site.register(User, UserAdmin)
admin.site.register([Dataset, Sample])


Btw, something like this is what we talk about when we ask for an excerpt of the code with only the relevant parts, please take the time to prepare things by stripping code unrelated to the issue at hand before posting your code. Help us to help you.

The above setup results in the following Change User view:

Inline image 1

Hoping this is of help,

--
Ramiro Morales
@ramiromorales
Reply all
Reply to author
Forward
0 new messages