views:

157

answers:

2

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?

A: 

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.

Haes
Nope, didn't help, but thanks for trying! I didn't manage to find that ticket before. Alas, it refers to the oldforms admin, which seems to have been completely rewritten a while ago...
TomA
+1  A: 

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

Alex Gaynor
First tried it with 1.0.2, now going to try 1.1 (on a live server so it will take a while to set things up properly).
TomA
Works perfectly with 1.1
TomA
As a sidenote - I guess I just needed a little push to do the 1.1 upgrade on the live server. Only a few Django sites but still... well fortunately nothing broke in the process.
TomA