Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate function names in the Django admin?

When using list_display as described under http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display you can not only display fields but custom callables as well:

def colored_name(self):
    return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name)
colored_name.allow_tags = True

And then use it like this:

list_display = ('first_name', 'last_name', 'colored_name')

Since first_name and last_name are normal fields we can just translate them like that:

first_name = models.CharField(_('first name'))
last_name = models.CharField(_('last name'))

So the question is:

How can I translate the name of my function? Where do I put my _('colored name')?

like image 672
Semmel Avatar asked Jul 05 '11 11:07

Semmel


People also ask

How do I translate text in Django?

Use the function django. utils. translation. gettext_noop() to mark a string as a translation string without translating it.

What does admin do in Django?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

Can I use Django admin in production?

your company should follow a least access principle policy; so yes: only select people should have access. Django has basic auditing capability via signals and displayed in the admin out of the box. You can build on top of this.


1 Answers

The example on the page that you linked to shows that the callable can have an attribute short_description, which is the string used as the title of the column. I haven't checked, but I strongly suspect that if you set that to a translatable string then it will work.

def colored_name(self):
    return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name)
colored_name.allow_tags = True
colored_name.short_description = _("Colored Name")
like image 187
Andrew Wilkinson Avatar answered Oct 07 '22 01:10

Andrew Wilkinson