Custom app names in the django admin
EDIT: This approach is flawed – it will never work in the app_index page (/admin/appname/) and can cause problems with contenttypes queries. You are better off overriding the admin templates (lots of them unfortunately). To avoid hardcoding the app_label in the templates wrap it in
transtags and use the internationalization framework to map the internal name to your desired display name.
EDIT 2: You can also use this.
Suppose you have a model like this:
class Stuff(models.Model):
class Meta:
verbose_name = u'The stuff'
verbose_name_plural = u'The bunch of stuff'
...
You have verbose_name, however you want to customise app_label too for different display in admin. Unfortunatelly having some arbitrary string (with spaces) doesn’t work and it’s not for display anyway.
Turns out that the admin uses app_label.title() for display so we can make a little hack: str subclass with overriden title method:
class string_with_title(str):
def __new__(cls, value, title):
instance = str.__new__(cls, value)
instance._title = title
return instance
def title(self):
return self._title
__copy__ = lambda self: self
__deepcopy__ = lambda self, memodict: self
Now we can have the model like this:
class Stuff(models.Model):
class Meta:
app_label = string_with_title("stuffapp", "The stuff box")
# 'stuffapp' is the name of the django app
verbose_name = 'The stuff'
verbose_name_plural = 'The bunch of stuff'
...
and the admin will show “The stuff box” as the app name.
The best approach so far:)
Interesting, but does that mean that for every model inside module, app_label should be added?
Yes, if you want custom display of the application name in the admin.
can’t use it, got:
TypeError at /admin/myappname/category/add/
__new__() takes exactly 3 arguments (2 given)
when try to add item
Looks like you forgot to pass the second argument (the title).
in app_label i write:
app_label = string_with_title(“myappname”, “APP NAME I WANT TO DISPlAY”)
class string_with_title(str) i insert after all imports, as first class in models.py
Odd, what python version are you using? Can you show me the traceback?
Python 2.7.2, Django 1.3
Trace: http://snipt.org/xkmnl
That’s caused by missing __copy__ methods. I’ve added those in the post. However, playing with this a bit more, I’ve found that this approach is totally flawed. See the post edit for an alternate solution.
Thanks for help!
Hi, custom app names in the admin will be possible with the app-loading refactor. You can checkout the branch at https://github.com/jezdez/django/tree/app-loading . An example of how an app might look like: https://github.com/jezdez/django/blob/app-loading/tests/appcachetests/anothermodel_app/app.py
You need to set the verbose_name attribute in the meta class of the app. If you see any problems or need help, I’m more than happy to help out!
Genius!!
Nice hack!
Super!! Great work mate! Cheers!!!
Hi;
what about encoding?
instance = str.__new__(cls, value)
UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\u0131′ in position 15: ordinal not in range(128)
thanks
My approach is flawed. Better use https://github.com/bmihelac/django-app-name-translation-in-admin instead.
Hi;
but what about encodin?
instance = str.__new__(cls, value)
UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\u0131′ in position 15: ordinal not in range(128)
Thanks
Excellent job.One notice: i change title def so now i can work with greek (and not only) charsets def title(self):
return self._title.decode(‘utf8′)