Question

When using localized list of "choices" for a model field, the admin doesn't show the translated values in the list view.

Short example:

from django.utils.translation import ugettext_lazy as _

class OrderStates:
    STATES = (
        (STATE_NEW, _("New")),
        (STATE_CANCELLED, _("Cancelled")), )

class Order(models.Model):
    state = models.IntegerField(choices=OrderStates.STATES)
    # ..

class OrderAdmin(admin.ModelAdmin):
    list_display = [ 'id', 'state', 'address', 'user']
    # ..

admin.site.register(Order, OrderAdmin)

The localized versions of "New" and "Cancelled" show up correctly in the front-end and in the admin form when editing an order. But in the admin list view I get blank fields - regardless of the language I switch to, including English. Column names are fine.

This only happens with Python 2.3 (talk about niche questions). The choices display correctly everywhere with Python 2.5. I don't get any errors or warnings in neither.

Tried using ugettext instead of ugettext_lazy for the options, which didn't work. ugettext_noop sort of works - it at least shows the original english versions instead of blank fields.

Am I doing something wrong or is this a bug?

Was it helpful?

Solution

This is probably a bug somewhere in Django, not calling force_unicode on the item correctly. The original code you pasted is correct. You don't mention what Django version you're using, so I'd reccomend trying the latest 1.0.3 or 1.1 release to see if that happens to fix it, else check the ticket tracker to see if it's already been reported (note that if it hasn't been fixed yet it probably won't be at all, since 1.1 is the last version to support 2.3).

OTHER TIPS

try using:

import gettext as _

Though, that may break if some of your translations use non-ascii values. Actually, this should have been fixed some time ago, see Ticket #5287.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top