Django

Code

Ticket #3591 (assigned)

Opened 1 year ago

Last modified 1 month ago

add support for custom app_label and verbose_name

Reported by: jkocherhans Assigned to: adrian (accepted)
Component: Core framework Version: SVN
Keywords: Cc: simon@quo.com.au, robillard.etienne@gmail.com, akaihol+django@ambitone.com, tom@eggdrop.ch, v.oostveen@idcanet.com, dave.lowe@biola.edu
Triage Stage: Accepted Has patch: 1
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 0

Description

Once the hot club of france opens and we have more third party apps, the probability of clashing app names will increase. We can fix this by allowing users to define a custom app_label in their settings file. In addition, we can add a verbose_name for apps at the same time. The patch here also correctly assigns and app_label to models in a package as long as you import all of those models in the packages __init__.py This possible breaks quite a few internals if people are using them, and needs to wait until after 0.96 to be considered.

Here's an example:

from django.conf.directives import app

INSTALLED_APPS = (
    'django.contrib.auth', # allow the old syntax
    app('mypkg.auth', 'myauth', 'My cool auth app'), # and the new. (path, app_label, verbose_name)
)

Originally discussed in this thread

Attachments

custom-app-labels.diff (20.6 kB) - added by jkocherhans on 02/26/07 22:30:58.
A couple of internal tests are still broken, but admin, manage.py, etc. work
app_labels.diff (28.3 kB) - added by Vinay Sajip <vinay_sajip@yahoo.co.uk> on 05/03/07 12:32:02.
A new, more comprehensive (IMHO) patch
app_labels.2.diff (28.1 kB) - added by Vinay Sajip <vinay_sajip@yahoo.co.uk> on 05/03/07 16:24:15.
Minor tweaks (tidy-ups) to the last patch.
app_labels.3.diff (28.1 kB) - added by Vinay Sajip <vinay_sajip@yahoo.co.uk> on 05/03/07 16:24:20.
Minor tweaks (tidy-ups) to the last patch.
app_labels.4.diff (29.4 kB) - added by Vinay Sajip <vinay_sajip@yahoo.co.uk> on 05/08/07 03:34:15.
Updated patch to work cleanly against trunk revision r5171.
app_labels.5.diff (30.9 kB) - added by Vinay Sajip <vinay_sajip@yahoo.co.uk> on 05/08/07 04:59:26.
Replaces previous patch which was not created using svn diff - sorry.
app_labels.6.diff (32.1 kB) - added by Vinay Sajip <vinay_sajip@yahoo.co.uk> on 05/25/07 18:26:59.
An updated patch to cater for recent changes in trunk.
app_labels.7.diff (35.1 kB) - added by Vinay Sajip <vinay_sajip@yahoo.co.uk> on 10/04/07 16:45:14.
An updated patch to cater for recent changes in trunk. Applies to r6453.
app_labels.8.diff (35.1 kB) - added by brosner on 11/01/07 18:54:04.
updated to apply cleanly against r6635
app_labels.9.diff (34.0 kB) - added by Vinay Sajip <vinay_sajip@yahoo.co.uk> on 12/16/07 05:08:51.
Updated to apply cleanly against r6920.

Change History

02/26/07 22:30:58 changed by jkocherhans

  • attachment custom-app-labels.diff added.

A couple of internal tests are still broken, but admin, manage.py, etc. work

02/26/07 22:31:56 changed by jkocherhans

  • status changed from new to assigned.
  • needs_better_patch set to 1.
  • needs_tests set to 1.
  • owner changed from adrian to jkocherhans.
  • needs_docs set to 1.
  • stage changed from Unreviewed to Design decision needed.

(follow-up: ↓ 3 ) 02/26/07 22:41:48 changed by mtredinnick

A couple of questions from an initial quick read:

  1. In management.py, why is the import change on line 366 (of the original) necessary? I think there's some subtlety escaping me there (if it's not necessary, it's probably worth removing so that we can tell the real changes from the stylistic ones).
  2. (this question left blank, because it was stupid and I worked out I was missing something obvious.)
  3. Do you really mean you have to import all models in an app's __init__. py (a big change and not ideal, but maybe inavoidable; I realise this is a bit of a tarpit) or just in app_name/models/__init__.py? The stuff in loading.py make me suspect you might mean the latter, but I might be reading it incorrectly.

(in reply to: ↑ 2 ) 02/26/07 23:08:46 changed by jkocherhans

Replying to mtredinnick:

A couple of questions from an initial quick read:

Hey Malcolm

First of all, thanks for taking a look at this, and second, none of this is meant to be final. This is the "get it working" version that I more or less only put up as a reference for another ticket that this fixes.

1. In management.py, why is the import change on line 366 (of the original) necessary? I think there's some subtlety escaping me

there (if it's not necessary, it's probably worth removing so that we can tell the real changes from the stylistic ones).

It has an effect. Changing the current import from
from django.db.models import get_models
from django.db.models import get_app, get_models to
would get the right effect.

3. Do you really mean you have to import all models in an app's __init__. py (a big change and not ideal, but maybe inavoidable; I realise this is a bit of a tarpit) or just in app_name/models/__init__.py? The stuff in loading.py make me suspect you might mean the latter, but I might be reading it incorrectly.

It's the latter. That could probably be worked around as well, but I haven't spent much time thinking about that part of it yet.

05/02/07 10:52:15 changed by Marty Alchin <gulopine@gamemusic.org>

It looks like we have a little bit of end-goal overlap with #4144. The patch over there is just a simple move of a single line, but it relates to how Django determines app_label, so it looks like that might be best handled in this ticket.

Essentially, this patch moves the model_module.__name__.split('.')[-2] into get_app_label, and in fact seems to remove any need for sys.modules at all (unless I'm reading something wrong). If the sys.modules line referenced in #4144 were to be removed as part of this ticket, #4144 would be unnecessary, and I'd be very happy.

05/03/07 12:32:02 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

  • attachment app_labels.diff added.

A new, more comprehensive (IMHO) patch

05/03/07 12:44:00 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

  • needs_better_patch deleted.
  • needs_tests deleted.
  • needs_docs deleted.

I've added an updated patch. This covers changes to make-messages.py and also documentation changes, and is against trunk revision r5146. All tests in runtests.py pass, plus browsing data from apps with a custom app label in admin and databrowse works fine. Here are the relevant changes to my settings.py:

from django.conf import app

# ...

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.humanize',
    'django.contrib.admin',
    'django.contrib.databrowse',
    'mysite.myapp',
    'mysite.registration',
    app('mysite.orghier', 'oh', 'Organization Hierarchy'),
)

05/03/07 14:06:17 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

Marty: Although my latest patch still leaves the line where it is, the model_module is not being used at all. I have removed the line entirely from my working copy - once I get other feedback on the patch, I will post another patch which incorporates the feedback.

05/03/07 16:24:15 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

  • attachment app_labels.2.diff added.

Minor tweaks (tidy-ups) to the last patch.

05/03/07 16:24:20 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

  • attachment app_labels.3.diff added.

Minor tweaks (tidy-ups) to the last patch.

05/08/07 03:34:15 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

  • attachment app_labels.4.diff added.

Updated patch to work cleanly against trunk revision r5171.

05/08/07 04:59:26 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

  • attachment app_labels.5.diff added.

Replaces previous patch which was not created using svn diff - sorry.

05/25/07 18:26:59 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

  • attachment app_labels.6.diff added.

An updated patch to cater for recent changes in trunk.

08/28/07 02:49:33 changed by anonymous

  • cc set to simon@quo.com.au.

09/14/07 14:18:21 changed by Chris H. <chris@heisel.org>

I'm seriously +1 on this -- at work we're hoping we'll be using a mix of our custom apps, apps built by other sites in our division and some of the stellar open source apps being built.

I think the modularity/plug-n-customize model of Django's "app" system is one of it's great features!

09/14/07 15:43:57 changed by adrian

I'm recording this idea here: What if INSTALLED_APPS itself was a *class*, rather than a tuple? That would solve a lot of the wacky issues.

09/15/07 19:13:34 changed by adrian

  • owner changed from nobody to adrian.
  • status changed from assigned to new.

09/15/07 19:13:43 changed by adrian

  • status changed from new to assigned.

09/16/07 08:43:36 changed by robillard.etienne@gmail.com

  • cc changed from simon@quo.com.au to simon@quo.com.au, robillard.etienne@gmail.com.

This patch is quite interesting.. ;-)

Would it make sense to use a dictionary and named arguments for making things more explicit?

In example:

INSTALLED_APPS = (
 app('apps.foobar', {'verbose_name': 'foobar app',
                     'app_label': 'foobar1'}),
)

09/16/07 10:02:20 changed by ubernostrum

#2982 was a duplicate.

09/16/07 11:07:59 changed by ubernostrum

#3343 needs to be dealt with when a resolution is reached here.

09/16/07 14:40:25 changed by ubernostrum

#4470 was a duplicate.

09/16/07 17:53:26 changed by Eli Courtwright <eli@courtwright.org>

Replying to ubernostrum:

#4470 was a duplicate.

That ticket would have fixed the problem of app_label being required when using the Django ORM outside of an actual Django application. I often write programs at my job which use the Django ORM for non-website programming, but I always have to add the Meta class to the model definition, which is ugly and looks like black magic to my co-workers who aren't familiar with Django internals:

class Something(models.Model):
    class Meta:
        app_label = ""
    
    name        = models.CharField(maxlength=100)
    description = models.TextField()
    quantity    = models.IntegerField()

This ticket doesn't seem to address this problem, whereas ticket #4470 did.

09/16/07 19:08:35 changed by ubernostrum

Eli, app_label is required to cleanly split models up into multiple files inside a models module. And I'm doing my best to keep all of the app_label-related stuff here in this ticket so we can get a clean solution that solves all of the associated issues.

09/16/07 20:31:10 changed by Eli Courtwright <eli@courtwright.org>

That makes sense. I just hope that whatever that clean solution is, it will allow us to define models outside of Django webapps without having to declare the Meta class and manually set app_label.

I'll try to find time this week to take a look at the work on this ticket so far and see how well that solves this problem, and offer any advice/patches I can come up with.

09/22/07 09:43:58 changed by Eli Courtwright <eli@courtwright.org>

The latest patch doesn't work against the current version of the Django trunk in svn. Nor does it work against r5171, which is the revision that patch 4 is said to work against, nor does it work against r5343, which was the latest revision on the date that the most recent patch was uploaded.

Someone needs to update the patch to work with the current version of Django. I won't have time to do this for at least the next few weeks, and I'm probably the wrong person to do it anyway, since I only use the Django ORM and don't write Django webapps. However, if no one else gets to this by the time I have some more free time, I'll take a stab at it.

09/22/07 18:19:26 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

I'm surprised that the last patch doesn't version with the earlier SVN version of Django - before uploading the patch, I had checked that all tests passed. Never mind - I'll try to look at this over the next week or two.

09/22/07 18:20:33 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

Oops, that should be "... last patch doesn't work ..."

10/04/07 16:45:14 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

  • attachment app_labels.7.diff added.

An updated patch to cater for recent changes in trunk. Applies to r6453.

10/04/07 16:46:21 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

Latest patch (against r6453) passes all tests (tests/runtests.py).

10/17/07 03:08:24 changed by akaihola

  • cc changed from simon@quo.com.au, robillard.etienne@gmail.com to simon@quo.com.au, robillard.etienne@gmail.com, akaihol+django@ambitone.com.

11/01/07 18:54:04 changed by brosner

  • attachment app_labels.8.diff added.

updated to apply cleanly against r6635

(follow-up: ↓ 26 ) 11/01/07 18:58:02 changed by brosner

I tested this patch and updated to apply to current trunk (r6635). All works for me.

However, I do have a question why get_installed_app_paths exists? Why can't it be done in __init__ of Settings when it expands out .* app paths? That way there isn't a need to change all instances of settings.INSTALLED_APPS. Making this completely backward compatible.

11/02/07 06:34:53 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

Good call - I'd done it the other way because it leaves INSTALLED_APPS untouched, and I wanted the impact of the patch to be clearly visible at least until the overall idea was approved. However, though it's been quite a long time - 8 months - since my first patch, and though there have been no adverse comments and a few comments with the gist "it just works", the devs have not seen fit to pronounce on it - so I'm treading water until I get a pronouncement about it. I just occasionally check to see if trunk changes break the patch, and update the patch accordingly. I'll merge your changes into mine so that my next patch has your changes, too.

(in reply to: ↑ 24 ) 11/07/07 09:30:11 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

Replying to brosner:

However, I do have a question why get_installed_app_paths exists? Why can't it be done in __init__ of Settings when it expands out .* app paths? That way there isn't a need to change all instances of settings.INSTALLED_APPS. Making this completely backward compatible.

I had another look at the get_installed_app_paths vs. INSTALLED_APPS issue. With my patch, INSTALLED_APPS can contain either package names (explicit or wild-card) or app instances. However, get_installed_app_paths always returns a set of strings - the package paths of the applications. This is used, as you've seen, in a lot of places. If a user puts an app instance into INSTALLED_APPS, I'm not sure they'd take kindly to having it automatically replaced with the corresponding path string. So, get_installed_app_paths insulates the rest of the code from having to know whether the INSTALLED_APPS entries are path strings or app instances. It seems to me that some kind of encapsulation will be needed - and get_installed_app_paths performs this function. I would like to be able to use INSTALLED_APPS and do away with get_installed_app_paths - but I'm not quite sure how, yet. If you provide a patch which sorts out this issue, I'll happy incorporate it, as I mentioned.

In your testing, did you have any app instances in your INSTALLED_APPS? I'd be interested in seeing what your INSTALLED_APPS looks like. From a quick inspection of your patch, I would expect some tests to fail if INSTALLED_APPS contained any app instances, because in some places where framework code expects a string, it would get an app instance.

12/01/07 17:37:01 changed by jacob

  • stage changed from Design decision needed to Accepted.

(follow-up: ↓ 29 ) 12/05/07 09:19:23 changed by wolfram

is #6080 not a partly shortcut?

12/16/07 05:08:51 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

  • attachment app_labels.9.diff added.

Updated to apply cleanly against r6920.

(in reply to: ↑ 28 ) 12/18/07 02:41:19 changed by Vinay Sajip <vinay_sajip@yahoo.co.uk>

Replying to wolfram:

is #6080 not a partly shortcut?

No, I don't believe #6080 overlaps with this ticket. That ticket is to do with loading apps from eggs; this ticket allows for easy specification of an app_label to disambiguate apps which end in the same name (e.g. 'django.contrib.admin' clashing with 'myapp.mypackage.admin'), and allows verbose names with i18n support for use in the admin (e.g. 'Authentication/Authorization' rather than 'auth').

01/01/08 09:41:58 changed by Thomas Steinacher <tom@eggdrop.ch>

  • cc changed from simon@quo.com.au, robillard.etienne@gmail.com, akaihol+django@ambitone.com to simon@quo.com.au, robillard.etienne@gmail.com, akaihol+django@ambitone.com, tom@eggdrop.ch.

03/19/08 13:27:57 changed by mrts

I believe another feature that would improve admin index page usability a lot belongs conceptually here.

Suppose I have a project that contains 10 applications, each containing several models. Some of the apps are of primay importance, some are less important. Currently, there is no way to impose either ordering or hide app contents. Thus it's hard for users to discern important bits from non-important ones and confusion is guaranteed.

So I propose the following addition to this patch:

  • ordering: app('mypkg.auth', 'myauth', 'My cool auth app', weight=1) # heavier items sink to the bottom in admin index page
  • collapsing: app('mypkg.auth', 'myauth', 'My cool auth app', style={'classes' : ('collapse',)}) # similar behaviour to fieldsets in admin index page

03/20/08 07:59:55 changed by mrts

03/27/08 17:03:35 changed by anonymous

  • cc changed from simon@quo.com.au, robillard.etienne@gmail.com, akaihol+django@ambitone.com, tom@eggdrop.ch to simon@quo.com.au, robillard.etienne@gmail.com, akaihol+django@ambitone.com, tom@eggdrop.ch, v.oostveen@idcanet.com.

04/10/08 18:38:41 changed by anonymous

  • cc changed from simon@quo.com.au, robillard.etienne@gmail.com, akaihol+django@ambitone.com, tom@eggdrop.ch, v.oostveen@idcanet.com to simon@quo.com.au, robillard.etienne@gmail.com, akaihol+django@ambitone.com, tom@eggdrop.ch, v.oostveen@idcanet.com, dave.lowe@biola.edu.

Add/Change #3591 (add support for custom app_label and verbose_name)




Change Properties
Action