Django permissions are untranslatableΒΆ

Tags: django

I was adding translations to my Django application and got a weird-looking error when running my tests:

Traceback (most recent call last):
  ...
  File ".../django/db/backends/sqlite3/base.py", line 238, in execute
    return Database.Cursor.execute(self, query, params)
pysqlite2.dbapi2.InterfaceError: Error binding parameter 0 - probably unsupported type.

Googling showed some mailinglist activity from 2006 and a five year old long-closed Django bug. So I must be doing something really wrong here :-)

Luckily we’re all using open source, so (after a tip from a colleague) I put some print statements into django/db/backends/sqlite3/base.py. A couple of the the queries and params passed into execute() were:

Q: INSERT INTO "auth_permission" ("name", "content_type_id", "codename") VALUES (?, ?, ?)
P: (u'Can change User group', 11, u'change_usergroup')
Q: INSERT INTO "auth_permission" ("name", "content_type_id", "codename") VALUES (?, ?, ?)
P: (u'Can delete User group', 11, u'delete_usergroup')
Q: INSERT INTO "auth_permission" ("name", "content_type_id", "codename") VALUES (?, ?, ?)
P: (<django.utils.functional.__proxy__ object at 0x1ce3ab0>, 11, 'can_view_lizard_data')

Hey! That last one looks different! Ah, that’s my newly-added translation. I had something like this inside my models.py:

from django.db import models
from django.utils.translation import ugettext_lazy as _

class UserGroup(models.Model):
    ...
    class Meta:
        permissions = (
            ('can_view_lizard_data', _('Can view lizard data')),
            #                       ^^^ note the _() translation marker.
            )

I was translating the permission’s friendly name. Turns out, those permission names cannot be translated as they’re database content instead of strings inside Python code. See the explanation in Django ticket 13861.

Learned another thing today :-)

Apartment construction in Utrecht
blog comments powered by Disqus
 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):