Opened 11 years ago

Closed 11 years ago

#21622 closed New feature (duplicate)

Support consistent customization of app_label

Reported by: Chris Spencer Owned by: nobody
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The app_label model meta attribute effects how both a table's name appears in the database as well as how the app's name appears in admin. Currently, it's very difficult to separate the "logical" usage of app_label from the "aesthetic" usage, the way we can with the db_table and verbose_name meta attributes for individual model names.

An example of when we might want to do this is when we want to create a django package where a non-Django package already exists. e.g. Say we want to create an admin interface for managing RSS feeds and call it "djangofeeds" but there's already a Python package called "feeds". We'd want the app_label to mirror the logical Python package name, but have it appear in admin as simply "Feeds" for clarity.

Another example when this would be useful is for renaming of the aesthetic appearance of an existing app without effecting the database. Currently, if you change app_label on an app with existing data, this also changes all the names of your tables, causing South migrations to delete your old tables along with all your data.

A partial solution to this problem is to use a class that overrides str.title, like:

    class StringWithTitle(str):
        """
        String class with a title method. Can be used to override 
        admin app names.

        http://ionelmc.wordpress.com/2011/06/24/custom-app-names-in-the-django-admin/
        """

        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

    class MyModel(models.Model):

        <columns>

        class Meta:
            app_label = StringWithTitle('my_app_label', 'My Fancy App Label')

However, in order for this to work everywhere in admin, all renderings of app_label would have to call title(). Unfortunately, admin renders app_labels inconsistently. In many places it calls title() but in others it calls capfirst().

If all renderings of app_label used title(), this would resolve the problem, and it would be a very simple alternative solution to ticket https://code.djangoproject.com/ticket/3591.

Change History (2)

comment:1 by Chris Spencer, 11 years ago

Submitted pull request https://github.com/django/django/pull/2080 implementing this change.

comment:2 by Preston Holmes, 11 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #3591 - which is once again being actively worked on again with a more narrow focus just a few things - this being one of main ones.

Note: See TracTickets for help on using tickets.
Back to Top