Home > Uncategorized > Custom app names in the django admin

Custom app names in the django admin

June 24, 2011

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 trans tags 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.

About these ads
Tags: ,
  1. teserak
    June 25, 2011 at 9:49 am

    The best approach so far:)

  2. June 25, 2011 at 3:49 pm

    Interesting, but does that mean that for every model inside module, app_label should be added?

    • Ionel Maries
      June 25, 2011 at 3:50 pm

      Yes, if you want custom display of the application name in the admin.

  3. artbatic
    June 26, 2011 at 7:05 pm

    can’t use it, got:

    TypeError at /admin/myappname/category/add/
    __new__() takes exactly 3 arguments (2 given)

    when try to add item

    • Ionel Maries
      June 26, 2011 at 7:12 pm

      Looks like you forgot to pass the second argument (the title).

  4. artbatic
    June 26, 2011 at 8:05 pm

    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

    • Ionel Maries
      June 26, 2011 at 8:06 pm

      Odd, what python version are you using? Can you show me the traceback?

  5. artbatic
    June 26, 2011 at 9:01 pm

    Python 2.7.2, Django 1.3

    Trace: http://snipt.org/xkmnl

    • Ionel Maries
      June 26, 2011 at 10:16 pm

      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.

  6. artbatic
    June 26, 2011 at 10:27 pm

    Thanks for help!

  7. June 27, 2011 at 1:54 pm

    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!

  8. September 27, 2011 at 1:57 am

    Genius!!

  9. November 23, 2011 at 2:33 pm

    Nice hack!

  10. Martin
    January 19, 2012 at 11:48 am

    Super!! Great work mate! Cheers!!!

  11. yassaman
    May 6, 2012 at 12:07 am

    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

  12. yassaman
    May 6, 2012 at 12:08 am

    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

  13. Chris Pappas
    April 12, 2013 at 4:43 am

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

  1. July 28, 2011 at 4:04 pm
Comments are closed.
Follow

Get every new post delivered to your Inbox.

Join 172 other followers