#3591 closed New feature (fixed)
add support for custom app_label and verbose_name
Reported by: | jkocherhans | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
Severity: | Normal | Keywords: | app-loading |
Cc: | dirleyrls, domen@…, Mike Scott, robillard.etienne@…, akaihol+django@…, tom@…, v.oostveen@…, dave.lowe@…, research@…, semente@…, gonz@…, ega641@…, mocksoul@…, hv@…, lidaobing@…, remco@…, s.angel@…, drackett@…, gabor@…, seocam@…, django@…, mbeachy@…, camillobruni@…, andy@…, bronger@…, mike@…, the.paper.men@…, mmitar@…, Chris Chambers, aljosa.mohorovic@…, sehmaschine@…, bendavis78@…, Jari Pennanen, mathijs@…, basti@…, JMagnusson, sfllaw@…, mgventura, nils@…, carlos.palol@…, sebastian.goll@…, ionel.mc@…, aav, danols@…, Sander Steffann, pshields@…, flisky, jakub@…, albrecht.andi@…, JonathanBarratt, marc.tamlyn@…, flavio.curella@…, 4glitch@…, luc.saffre@…, zachborboa+django@…, vlastimil@…, django@…, akanouras, unpig, ua_django_bugzilla@…, someuniquename@…, Daniel Samuels | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
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 (17)
Change History (174)
by , 18 years ago
Attachment: | custom-app-labels.diff added |
---|
comment:1 by , 18 years ago
Needs documentation: | set |
---|---|
Needs tests: | set |
Owner: | changed from | to
Patch needs improvement: | set |
Status: | new → assigned |
Triage Stage: | Unreviewed → Design decision needed |
follow-up: 3 comment:2 by , 18 years ago
A couple of questions from an initial quick read:
- 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).
- (this question left blank, because it was stupid and I worked out I was missing something obvious.)
- 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 inapp_name/models/__init__.py
? The stuff inloading.py
make me suspect you might mean the latter, but I might be reading it incorrectly.
comment:3 by , 18 years ago
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.
- 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.
- 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 inapp_name/models/__init__.py
? The stuff inloading.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.
comment:4 by , 18 years ago
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.
comment:5 by , 18 years ago
Needs documentation: | unset |
---|---|
Needs tests: | unset |
Patch needs improvement: | unset |
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'), )
comment:6 by , 18 years ago
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.
by , 18 years ago
Attachment: | app_labels.4.diff added |
---|
Updated patch to work cleanly against trunk revision r5171.
by , 18 years ago
Attachment: | app_labels.5.diff added |
---|
Replaces previous patch which was not created using svn diff - sorry.
by , 18 years ago
Attachment: | app_labels.6.diff added |
---|
An updated patch to cater for recent changes in trunk.
comment:7 by , 17 years ago
Cc: | added |
---|
comment:8 by , 17 years ago
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!
comment:9 by , 17 years ago
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.
comment:10 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
comment:11 by , 17 years ago
Status: | new → assigned |
---|
comment:12 by , 17 years ago
Cc: | added |
---|
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'}), )
comment:16 by , 17 years ago
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.
comment:17 by , 17 years ago
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.
comment:18 by , 17 years ago
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.
comment:19 by , 17 years ago
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.
comment:20 by , 17 years ago
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.
by , 17 years ago
Attachment: | app_labels.7.diff added |
---|
An updated patch to cater for recent changes in trunk. Applies to r6453.
comment:23 by , 17 years ago
Cc: | added |
---|
follow-up: 26 comment:24 by , 17 years ago
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.
comment:25 by , 17 years ago
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.
comment:26 by , 17 years ago
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.
comment:27 by , 17 years ago
Triage Stage: | Design decision needed → Accepted |
---|
comment:29 by , 17 years ago
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').
comment:30 by , 17 years ago
Cc: | added |
---|
comment:31 by , 17 years ago
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
comment:33 by , 17 years ago
Cc: | added |
---|
comment:34 by , 17 years ago
Cc: | added |
---|
comment:35 by , 16 years ago
Cc: | added |
---|
follow-up: 37 comment:36 by , 16 years ago
I'm strongly -1 on any admin specific arguments to app(). This even includes verbose_name.
IMO the biggest achievement in nfa is that "all admin functionality has been decoupled from the model syntax" (NewformsAdminBranch).
Don't let such a coupling creep in again through app().
follow-up: 38 comment:37 by , 16 years ago
Replying to emulbreh:
I'm strongly -1 on any admin specific arguments to app(). This even includes verbose_name.
IMO the biggest achievement in nfa is that "all admin functionality has been decoupled from the model syntax" (NewformsAdminBranch).
Don't let such a coupling creep in again through app().
So what mechanism would you propose for displaying e.g. an internationalized name for an application in the admin?
comment:38 by , 16 years ago
Replying to Vinay Sajip <vinay_sajip@yahoo.co.uk>:
So what mechanism would you propose for displaying e.g. an internationalized name for an application in the admin?
Something analogous to ModelAdmin
classes:
class MyAppAdmin(AppAdmin): app = 'mypkg.myapp' verbose_name = _('My App') description = _('...') style = {'classes' : ('collapse',)} weight = 1 admin.site.register(MyAppAdmin)
AppAdmin
classes could be implicitly created for apps that don't provide one.
I'm not familar with nfa yet (as I'm currently using trunk without admin), so this might be a painfull change.
But it should be doable with a little refactoring (app level permission code then should probably move to AppAdmin
as well).
comment:40 by , 16 years ago
Cc: | added |
---|
comment:41 by , 16 years ago
emulbreh has a good point. The app()
directive is really only required for admin, so it does not conceptually belong to settings.py
. Perhaps this should be discussed further on django-developers.
comment:42 by , 16 years ago
At the Pycon sprint there was a rather long discussion about that which later got documented at InstalledAppsRevision
comment:43 by , 16 years ago
I should also throw out there that while Django itself might only use these options in the admin, other apps may make good use of them as well. My dbsettings app, for instance, shows application names on its configuration screen, and I'd rather not reach into the guts of the admin just to get the app's verbose_name
. After all, verbose_name
isn't being moved out into an admin directive in newforms-admin, is it? I don't see why an app's verbose_name
should be treated any differently.
comment:44 by , 16 years ago
I think we should think of this matter not as a problem with the admin as a general inflexibility of the app loading mechanism.
This results, for example, in the weird behaviour that only apps with a models.py are loaded correctly. I think it's worthwhile to make the app loading happen in a more generic representation, like an App class, just like suggested in InstalledAppsRevision. Such an App could very well include a verbose_name (and other meta data) that would be found by Django and 3rd party apps.
comment:45 by , 16 years ago
Admittedly I was wrong about app()
's usefulness only in admin context. path
, name
and db_prefix
parameters listed in InstalledAppsRevision should be generally available. style
and weight
(order
is perhaps a better name) are evidently admin-specific. description
lies somewhere in the middle -- it may be useful in other contexts besides admin.
Looks like both app()
and something in the lines of AppAdmin
are necessesary. Once the presentation layer has been decoupled with AppAdmin
, other clever ways for building even more flexible custom admin interfaces utilizing it are free to emerge. Coupling presentation to app()
on the other hand is a dead end. So I'm -1 to my own proposal to extend app()
with additional keywords, but still +1 for a flexible mechanism for app presentation in the admin index page.
comment:48 by , 16 years ago
milestone: | 1.0 beta → post-1.0 |
---|
We've passed feature-freeze (mostly), so I'm pushing this post-1.0. :(
comment:49 by , 16 years ago
Cc: | added |
---|
comment:50 by , 16 years ago
It should go far beyond what's outlined in InstalledAppsRevision. There's a lot to gain, e.g. no more magic in models autodiscovery, app dependencies etc. Apps should be packaged as eggs and the setuptools config structure setuptools.setup
should be extended in Django-specific way. I'll eventually write a spec draft and bring it up in django-developers after 1.0 is out.
comment:51 by , 16 years ago
Cc: | added |
---|
comment:52 by , 16 years ago
Another point: think of a way to expose app media (js, css, images) as well.
comment:53 by , 16 years ago
Cc: | added |
---|
by , 16 years ago
Attachment: | app_labels.10.diff added |
---|
Updated to apply cleanly against r8965 (post 1.0).
comment:54 by , 16 years ago
This latest patch (against r8965) passes all tests (tested with PostgreSQL backend). Please try it out and post your feedback on the mailing lists! Hopefully now that Django 1.0 is out (yay!), the core devs will have more time to review this patch and give some feedback on what's lacking...
comment:55 by , 16 years ago
Cc: | added |
---|
comment:56 by , 16 years ago
Cc: | added |
---|
comment:57 by , 16 years ago
Cc: | added |
---|
comment:58 by , 16 years ago
Cc: | added |
---|
comment:59 by , 16 years ago
is adrian still around? it's been more than one year since he's last posted here.
no offense intended but maybe it would bring this bug back to life if it weren't assigned?
comment:60 by , 16 years ago
Cc: | added |
---|
comment:61 by , 16 years ago
Cc: | added |
---|
by , 16 years ago
Attachment: | app_labels.12.diff added |
---|
Patch updated to expose verbose_name in admin; fixed bug in auth; should apply cleanly to r9498
comment:62 by , 16 years ago
Updated Vinay's patch so that the admin views expose the app's verbose_name as app_label in the templates; also fixed a bug in django/contrib/auth/management/__init__.py
that prevents create_superuser
to be called in syncdb.
Applies cleanly to r9498, the latest trunk revision before the the 1.0.2 release. Hope some core-dev takes a look at it and gives feedback before 1.1.
comment:63 by , 16 years ago
Cc: | added |
---|
comment:64 by , 16 years ago
Cc: | added |
---|---|
Resolution: | → fixed |
Status: | assigned → closed |
comment:65 by , 16 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
Please don't close tickets that aren't fixed yet.
comment:67 by , 16 years ago
Cc: | added |
---|
comment:68 by , 16 years ago
Cc: | added |
---|
comment:69 by , 16 years ago
Cc: | added |
---|
comment:70 by , 15 years ago
Cc: | added |
---|
comment:71 by , 15 years ago
Cc: | added |
---|
comment:72 by , 15 years ago
Cc: | added |
---|
comment:73 by , 15 years ago
Cc: | added |
---|
comment:74 by , 15 years ago
Cc: | added |
---|
comment:75 by , 15 years ago
Triage Stage: | Accepted → Design decision needed |
---|
Design Decision still needed according to Alex Gaynor
comment:76 by , 15 years ago
Cc: | added |
---|
comment:77 by , 15 years ago
Cc: | added |
---|
comment:78 by , 14 years ago
Cc: | added |
---|
comment:79 by , 14 years ago
Cc: | added |
---|
comment:80 by , 14 years ago
Triage Stage: | Design decision needed → Accepted |
---|
Accepted on the basis that this was a GSoC 2010 project, and the patch is looking reasonably good.
comment:81 by , 14 years ago
Triage Stage: | Accepted → Fixed on a branch |
---|
Sorry - to clarify: The patch that looks good is the GSoC branch, not the patch on this ticket.
comment:82 by , 14 years ago
Cc: | added |
---|
comment:83 by , 14 years ago
Cc: | added |
---|
comment:84 by , 14 years ago
Cc: | added |
---|
comment:85 by , 14 years ago
Cc: | added |
---|
comment:86 by , 14 years ago
Cc: | added |
---|
comment:87 by , 14 years ago
Cc: | added |
---|
comment:88 by , 14 years ago
I don't know what is the current suggested syntax for this, but either way it should be defined inside the app, and not during installation of app like in the example of description. E.g. if we allow the app_label be variable that can be defined in INSTALLED_APPS
, what happens to all permission checks? Someone installs the auth
app as myauth
and suddenly all permission checks stops working.
Probably it should be in the __init__.py
of app, with something like:
APP_LABEL='myauth'
This way all application developers should define APP_LABEL
in the __init__.py
and they could use it like namespace, with values like ciantic_myauth
. It would also allow backwards compatible way to do this.
comment:89 by , 14 years ago
Cc: | removed |
---|
comment:90 by , 14 years ago
Does it make sense to allow the overriding of app_label? It seems app_label should only be used internally by django to differentiate between apps. I'm not sure where the need is for overriding app_label in the first place. Wouldn't something like this be enough?
INSTALLED_APPS = ( 'some.foo.app', app('another.app', name='My App'), )
That way models can be grouped together by name in the admin. One reason I have a need for this is we have a blog app called "zinnia" installed, which has models called "Entries". It uses the django-comments app, which puts all of its models under "comments". Ideally, I should have in the admin an app area called "Blog", with models for "Entries" and "Comments", even though those models live in different apps.
comment:91 by , 14 years ago
Please have a look at the app-loading branch created during the GSoC 2010 which is scheduled to be included in 1.4.
comment:92 by , 14 years ago
Okay I think I found something from the branch. There will be App class for each app. This is a good thing, the verbose_name and app_label will be defined inside the app, which is the right thing to do. See the apps.py in GSoC 2010 branch which allowes one to define verbose_name etc. Basically like defining new App class for each app in __init__.py
or anywhere for that matter. After which the INSTALLED_APPS is just list of App classes.
Btw it would be helpful if there were a README or something considering the branch, so one would know what and how the branch proposes to implement the feautre. Now I just "luckily" guessed the place for this apps.py
.
comment:93 by , 14 years ago
milestone: | → 1.4 |
---|
comment:95 by , 14 years ago
Cc: | added |
---|
comment:96 by , 14 years ago
Cc: | added |
---|
comment:97 by , 14 years ago
Cc: | added |
---|
comment:98 by , 14 years ago
Cc: | removed |
---|
comment:99 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:100 by , 14 years ago
Cc: | added |
---|
comment:101 by , 14 years ago
Cc: | added |
---|
comment:102 by , 13 years ago
Cc: | added |
---|---|
Easy pickings: | unset |
comment:103 by , 13 years ago
Easy pickings: | set |
---|
comment:105 by , 13 years ago
Cc: | added |
---|---|
UI/UX: | unset |
Is anyone actively working on this? I'm willing to help bring the soc2010/app-loading branch up to date with trunk, if help is needed.
comment:106 by , 13 years ago
UI/UX: | set |
---|
comment:107 by , 13 years ago
@bendavis78: I am pretty sure your help would be greatly appreciated - considering the amount of attention/CC's for this ticket.
Maybe get on IRC and discuss with one or two of the core devs as to how and whether your work might be merged into trunk as soon as possible - and as to what the bottlenecks and design goals are for this feature. -- irc://irc.freenode.net/django
comment:108 by , 13 years ago
@bendavis78 Yes, arthurk and me were working on this ticket till a month ago, see https://github.com/jezdez/django/tree/app-loading for the latest state.
follow-up: 110 comment:109 by , 13 years ago
Thanks. For clarity's sake, I'm attaching a patch based on jezdez's app-loading branch that applies cleanly against r16630 (current svn trunk). I also have this on my github fork here: https://github.com/savidworks/django/tree/app-loading.
Also, for the benefit of others interested in this patch, I'll do my best to explain what it does based on what I've gleaned from the source:
The INSTALLED_APPS
setting may now contain a reference to an
App
class. For example:
INSTALLED_APPS = ( # ... 'django.contrib.admin', 'myapp.apps.MyApp', )
These app classes extend django.apps.App
, and may define a number of properties:
from django.apps import App class MyApp(App): class Meta: verbose_name = 'My sweet pony app'
Other properties that can be set on Meta are db_prefix
and
models_path
. This will allow you to override existing app definitions in order to prevent potential conflicts.
When the app loader loops through INSTALLED_APPS
, it converts any normal modules an
App
class and loads everything into the apps cache (which was moved from
django.models.loading
to
django.apps.cache
). This cache is, among other things, used by the admin for introspection of apps and models.
For further customization, one may also specify how an App
class is initialized by providing kwargs in the
INSTALLED_APPS
setting:
INSTALLED_APPS = ( # ... ('myapp.apps.MyApp', { 'verbose_name':'Something else', 'foo':'bar' }), )
In this case, MyApp
would be initialized to the equivalent of:
class MyApp(App): foo = 'bar' class Meta: verbose_name = 'Something else'
My only question at this point is regarding the design decision to use an options class (ie, MyApp.Meta
). The code suggests that other properties may be set on your custom App class, but how might one use custom properties on an App class? I'm sure there's a reason for not putting verbose_name, db_prefix, and models_path in the class's main properties, I'm just curious what the thinking is on that.
On another note, I was thinking it would also be useful to allow app authors to define how apps should be displayed to the end user. Specifically, I think it would be nice to be able to group and order models in a way that would be more meaningful or helpful to admin users. Perhaps that's one thing that could be done on within a custom App class. Although, that might be a discussion for another time.
comment:110 by , 13 years ago
Replying to bendavis78:
Thanks. For clarity's sake, I'm attaching a patch based on jezdez's app-loading branch that applies cleanly against r16630 (current svn trunk). I also have this on my github fork here: https://github.com/savidworks/django/tree/app-loading.
Actually that wasn't really worth the hassle, you merged it wrong (left some code from old commits in admin/options.py
). I attached a correct patch.
For further customization, one may also specify how an
App
class is initialized by providing kwargs in the
INSTALLED_APPS
setting:
INSTALLED_APPS = ( # ... ('myapp.apps.MyApp', {'verbose_name':'Something else','foo':'bar'}), )
FWIW, I like the following notation better:
INSTALLED_APPS = ( # ... ('myapp.apps.MyApp', { 'verbose_name': 'Something else', 'foo': 'bar', }), )
My only question at this point is regarding the design decision to use an options class (ie,
MyApp.Meta
). The code suggests that other properties may be set on your custom App class, but how might one use custom properties on an App class? I'm sure there's a reason for not putting verbose_name, db_prefix, and models_path in the class's main properties, I'm just curious what the thinking is on that.
It's simple, Meta
is used internally by Django during startup, so it needs to be handled with care, reducing the chance to break a rather fundamental part. In other words, it's internal API when it comes to app loading and is configured depending on the (Python) meta class AppBase
. Using it in app-specific implementation details like looking for a specific module in all apps (say for implementing the discovery pattern) is fine though. If you want to override/set a specific option of an app class we need to differ between those internal (via _meta
) and the "usual" class or instance attributes.
On another note, I was thinking it would also be useful to allow app authors to define how apps should be displayed to the end user. Specifically, I think it would be nice to be able to group and order models in a way that would be more meaningful or helpful to admin users. Perhaps that's one thing that could be done on within a custom App class. Although, that might be a discussion for another time.
That's certainly a good idea but not really part of this ticket. In other words, this can be added at a later time on top of the new app classes.
comment:111 by , 13 years ago
Ah, thanks for catching my mistake in the merge.
It looks like my assumption that an App subclass can implement its own attributes is incorrect with the current patch. Currently the python metaclass AppBase does not propagate the class's attrs to the new_class It only propagates {'__module__': module}
:
def __new__(cls, name, bases, attrs): super_new = super(AppBase, cls).__new__ parents = [b for b in bases if isinstance(b, AppBase)] if not parents: # If this isn't a subclass of App, don't do anything special. return super_new(cls, name, bases, attrs) module = attrs.pop('__module__', None) new_class = super_new(cls, name, bases, {'__module__': module}) attr_meta = attrs.pop('Meta', None) if not attr_meta: meta = getattr(new_class, 'Meta', None) else: meta = attr_meta app_name = attrs.pop('_name', None) if app_name is None: # Figure out the app_name by looking one level up. # For 'django.contrib.sites.app', this would be 'django.contrib.sites' app_module = sys.modules[new_class.__module__] app_name = app_module.__name__.rsplit('.', 1)[0] new_class.add_to_class('_meta', AppOptions(app_name, meta)) # For easier Meta inheritance new_class.add_to_class('Meta', attr_meta) return new_class
Thoughts? Should App subclasses not be allowed to have any attributes other than Meta or _name?
comment:112 by , 13 years ago
No, you misunderstood me, the class attributes of the app classes shouldn't be put into _meta since they are not part of the internal API. This follows the same pattern as the ModelForm
and Model
classes .
comment:113 by , 13 years ago
https://github.com/jezdez/django/blob/app-loading/django/apps/base.py#L50-55 shows pretty well how it's supposed to work:
def __init__(self, **options): for key, value in options.iteritems(): if key in DEFAULT_NAMES: setattr(self._meta, key, value) else: setattr(self, key, value)
Any parameter which is passed during initialization to the app class (e.g. from the settings) ought to be set as an instance attribute, unless it's one of the few internal options. It's a simple matter of easy customization to be able to do that.
comment:114 by , 13 years ago
Cc: | removed |
---|
comment:115 by , 13 years ago
Cc: | removed |
---|
comment:116 by , 13 years ago
Sure, I understand that they shouldn't be passed into _meta. But one would expect any custom attributes on their app class to stay there, and not magically disappear, correct? For example, if I create this:
class MyApp(App): foo = "bar"
the foo
attribute of the class will magially disappear. This happens because in the python metaclass, new_class is not being given the attributes of the original class.
by , 13 years ago
Attachment: | app-loading.2.diff added |
---|
comment:117 by , 13 years ago
Yeah, apologies, you're absolutely right, I've pushed a fix and attached an updated patch.
follow-up: 121 comment:118 by , 13 years ago
I noticed that in the admin, app titles are not run through .title(), as they were before the patch. It should be:
app_dict[app_label] = { 'name': apps.find_app(app_label)._meta.verbose_name.title(), 'app_url': app_label + '/', 'has_module_perms': has_module_perms, 'models': [model_dict],
(contrib/admin/sites.py, line 352)
comment:119 by , 13 years ago
Cc: | added |
---|
comment:121 by , 13 years ago
Replying to bendavis78:
I noticed that in the admin, app titles are not run through .title(), as they were before the patch.
Actually, the application label is, in current admin code, inconsistently run through either the capfirst
template filter (or its underlying function in view Python code) or tranformed by the .title()
method.
This would be a good chance to get that fixed and IMHO allowing the app author to define the capitalization of the label would be the best approach (now the label can be something more complex that the app module/package name e.g. "TLD Registries Contacts") instead of forcing such a basic treatment like .title()
to be always applied (same thing for translators of that label to other languages).
comment:122 by , 13 years ago
Cc: | added |
---|
comment:123 by , 13 years ago
Cc: | added |
---|
comment:125 by , 13 years ago
Cc: | added |
---|
comment:126 by , 13 years ago
Cc: | added |
---|
comment:127 by , 13 years ago
Cc: | added |
---|
comment:128 by , 13 years ago
Cc: | added |
---|
comment:129 by , 13 years ago
Cc: | added |
---|
comment:130 by , 13 years ago
Cc: | added |
---|
comment:131 by , 13 years ago
Cc: | added |
---|
comment:132 by , 12 years ago
Cc: | added |
---|
comment:133 by , 12 years ago
Cc: | added |
---|
comment:134 by , 12 years ago
2b9fb2e6443c04e4415b17083d727bd80047b6e5 fixed a deadlock in app loading. It does not contain tests as it seemed too hard to write one into Django's test suite. So, a heads up: the app-loading refactor should take care not to reintroduce the deadlock condition again. Refs #18251.
comment:135 by , 12 years ago
Cc: | added |
---|
comment:136 by , 12 years ago
Wanted to let you know I've started a discussion on the django developers group that is related to this ticket. I would appreciate your opinions regarding the topic and advice on how to make it go forward.
https://groups.google.com/forum/?fromgroups=#!topic/django-developers/7f0gKen2ces
Thank you in advance for the troubles.
comment:137 by , 12 years ago
Cc: | added |
---|
comment:138 by , 12 years ago
Triage Stage: | Fixed on a branch → Accepted |
---|
The GSoC branch is out of date.
The latest efforts are in the app-loading branch.
comment:139 by , 12 years ago
Cc: | added |
---|
comment:140 by , 12 years ago
Cc: | added |
---|
comment:141 by , 12 years ago
Status: | reopened → new |
---|
comment:142 by , 12 years ago
My suggestion: Let the developer declare in the application package's init.py something like:
class Meta: #or whatever the name should be.
verbose_name = _(u"App title for stuff like Admin")
... perhaps more stuff would be convenient here ...
Keepin' seein' the raw name in the admin interface just sucks :(.
comment:143 by , 12 years ago
Cc: | added |
---|
comment:144 by , 12 years ago
Cc: | added |
---|
comment:145 by , 11 years ago
Cc: | added |
---|
comment:146 by , 11 years ago
Cc: | added |
---|
comment:147 by , 11 years ago
Cc: | added |
---|
comment:148 by , 11 years ago
#21018 raised an interesting point about ordering in INSTALLED_APPS that should be tracked as part of this issue.
comment:149 by , 11 years ago
Patch needs improvement: | set |
---|
comment:150 by , 11 years ago
Cc: | added |
---|
comment:151 by , 11 years ago
Cc: | removed |
---|
comment:152 by , 11 years ago
Cc: | added |
---|
comment:154 by , 11 years ago
Patch needs improvement: | unset |
---|
comment:155 by , 11 years ago
Has patch: | unset |
---|
Django now supports a custom verbose_name (in master, which will become 1.7).
There's no patch available for the next steps.
comment:156 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
This ticket has become long and unwieldy.
Since the hardest part of the refactoring is done, I'm going to close it.
One of the two original features is implemented and the other is tracked in #21683.
I'm filing a series of smaller tickets for remaining or related tasks: https://code.djangoproject.com/query?status=!closed&keywords=~app-loading
comment:157 by , 11 years ago
Keywords: | app-loading added |
---|
A couple of internal tests are still broken, but admin, manage.py, etc. work