DDN: On portability of apps and using Meta.app_label

15 views
Skip to first unread message

Rob Hudson

unread,
Aug 21, 2007, 12:12:10 PM8/21/07
to Django developers
Hi Devs,

I'm working on a patch for #5177
(http://code.djangoproject.com/ticket/5177) which will clean up
orphaned content types and Malcolm questioned, "What happens if a
model has Meta.app_label set?" and further went on to explain that it
was never clearly determined if app_label should only refer to the
containing app.

If it did only refer to the containing app, then my patch is safe --
it will remove content type entries for models that no longer have a
matching model in an app. If it can refer to other apps, that means
one can inject a model into another application, and my patch could
potentially delete valid content types.

If I've gathered all my information correctly, app_label was orginally
added so one not need to have models in models.py and can break models
up into many files. (Changeset 2277) Setting Meta.app_label also
seems to be essential to dynamic models
(http://code.djangoproject.com/wiki/DynamicModels).

If I can summarize and email Malcolm sent to me, which is probably way
more coherent and correct than I could explain, he states the
following...

"""
There's a really annoying semi-bug in Django whereby if
you want split your models up into multiple files, say, a directory
structure along the lines of

application/
__init__.py
models/
__init__.py
modelA.py
more_models/ # Just to show it's not one-layer deep
__init__.py
modelB.py

then importing a model from application.models.modelA or whatever
doesn't work out of the box. So, you have to manually set the
Meta.app_label attribute on each model so that it knows how to work out
which app it belongs to.

[...]

That's all well and good. However, what isn't "clear" (as a way of
saying "we haven't made a declarative announcement about this") is
whether it's permissible to set app_label to something that isn't
"application" in the above example. Could I inject a model into another
app altogether by using this attribute?

If we decide that's really not supported and we won't guarantee it, then
that would be best. Because then we can one day fix the above situation
in a portable fashion -- the "portable" bit because people forget you
can have directories inside of directories -- and we can assume that all
of an application's models are initialised in a single call to a
post_syncdb-listening function.
"""

Thanks,
Rob

SmileyChris

unread,
Aug 21, 2007, 3:46:06 PM8/21/07
to Django developers
On Aug 22, 4:12 am, "Rob Hudson" <treborhud...@gmail.com> wrote:
> ...then importing a model from application.models.modelA or whatever

> doesn't work out of the box. So, you have to manually set the
> Meta.app_label attribute on each model so that it knows how to work out
> which app it belongs to.

By the way, the patch in http://code.djangoproject.com/ticket/4470
means you don't have to manually set it.

Reply all
Reply to author
Forward
0 new messages