Hi,

The process of model discovery is not modified by this patch.
Continue reading for more details.

- Mark

polymorphic is an interesting case, as it only defines one model and that
model is abstract. Therefore polymorphic doesn't need to be in
INSTALLED_APPS (and perhaps my example is therefore dumb). The way Django
currently works (in django.db.models.base.ModelBase) is that the app_label
for a model is determined by "looking one level" up from the module the
model is *defined* in. This has the interesting effect that models are
discovered by the mere fact of importing them; they don't need to be defined
in a module named models.

Currently when PolymorphicModel is imported from polymorphic (either by
writing "from polymorphic import PolymorphicModel" or
"from polymorphic.polymorphic_model import PolymorphicModel"; the class is
available in both modules), the app_label is determined to be "polymorphic"
since "polymorphic" is one level up from "polymorphic_model" in
"polymorphic.polymorphic_model", which is the module PolymorphicModel is
actually defined in.

My patch mimics this behavior.

As another example, consider a model defined in myapp.tests (this is done in
the tests provided with the patch). Models in myapp.tests are discovered
since myapp.tests is imported when running tests. app_label is determined to
be "myapp" in this case since "myapp" is one level up from tests in
"myapp.tests".

Now consider a model defined in myapp.models.some_models. app_label would
currently be determined to be "models" since "models" is one level up from
"some_models" in "myapp.models.some_models". This is not the desired
behavior, so a Meta class must be defined with the attribute app_label =
"myapp" to rectify the situation.

My patch makes is so that Django looks one level up from the last occurrence
of "models", if present. app_label then doesn't need to be explicitly
specified for every model when factoring a app's models module into sub
modules.

What's strange about all this is that if you currently (and with my patch)
define a model in myapp.package.other_models and other_models gets imported,
then SomeModel in other_models will have the bogus app_label "package" since
"package" is one level up from "other_models" in
"myapp.package.other_models". This turns out to be harmless most of the time
since sql isn't generated for apps that aren't in INSTALLED_APPS (although
you're in trouble if you actually try to use a model with a bogus
app_label). The one freaky case is when you actually have an app named
"package", in which case sql will be generated for the bogus models as well
as the expected models.

This strangeness is present is Django currently, and my patch doesn't fix of
modify this behavior.

- Mark

On Wed, Jul 28, 2010 at 9:46 AM, burc...@gmail.com <burc...@gmail.com>wrote:

> Hi Mark,
>
> > For 'polymorphic.polymorphic_model' it would be 'polymorphic'.
> Is that correct this didn't work at all (or didn't work properly)
> before your patch, and now works properly, so one can put
> "polymorphic.polymorphic_model" into their INSTALLED_APPS and
> everything would work?
> Should one import polymorphic_model from polymorphic.__init__ and put
> "polymorphic" into INSTALLED_APPS?
> Should one import polymorphic_model from polymorphic.models and put
> "polymorphic" into INSTALLED_APPS?
>
> In few words, how does the models discovery work for this case with your
> patch?
>
> --
> Best regards, Yuri V. Baburov, ICQ# 99934676, Skype: yuri.baburov,
> MSN: bu...@live.com
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers" group.
> To post to this group, send email to django-develop...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-developers+unsubscr...@googlegroups.com<django-developers%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-developers?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to