1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

magic-removal: elimated 'module._MODELS' and replaced with a register function,

so that setting 'app_label' is all that's needed for models in different files.


git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2278 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Luke Plant 2006-02-05 00:03:12 +00:00
parent adc562d42d
commit 6a90f49fe5
6 changed files with 14 additions and 16 deletions

View File

@ -151,7 +151,7 @@ def model_detail(request, app_label, model_name):
except ImproperlyConfigured: except ImproperlyConfigured:
raise Http404, "App %r not found" % app_label raise Http404, "App %r not found" % app_label
model = None model = None
for m in app_mod._MODELS: for m in models.get_models(app_mod):
if m._meta.object_name.lower() == model_name: if m._meta.object_name.lower() == model_name:
model = m model = m
break break

View File

@ -438,7 +438,7 @@ def get_admin_index(app):
app_label = app_models[0]._meta.app_label app_label = app_models[0]._meta.app_label
output.append('{%% if perms.%s %%}' % app_label) output.append('{%% if perms.%s %%}' % app_label)
output.append('<div class="module"><h2>%s</h2><table>' % app_label.title()) output.append('<div class="module"><h2>%s</h2><table>' % app_label.title())
for klass in mod._MODELS: for klass in app_models:
if klass._meta.admin: if klass._meta.admin:
output.append(MODULE_TEMPLATE % { output.append(MODULE_TEMPLATE % {
'app': app_label, 'app': app_label,

View File

@ -7,6 +7,7 @@ from django.db.models.query import orderlist2sql
from django.db.models.options import Options, AdminOptions from django.db.models.options import Options, AdminOptions
from django.db import connection, backend from django.db import connection, backend
from django.db.models import signals from django.db.models import signals
from django.db.models.loading import register_models
from django.dispatch import dispatcher from django.dispatch import dispatcher
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.utils.functional import curry from django.utils.functional import curry
@ -46,8 +47,7 @@ class ModelBase(type):
new_class._prepare() new_class._prepare()
# Populate the _MODELS member on the module the class is in. register_models(new_class._meta.app_label, new_class)
model_module.__dict__.setdefault('_MODELS', []).append(new_class)
return new_class return new_class
def cmp_cls(x, y): def cmp_cls(x, y):

View File

@ -6,6 +6,7 @@ from django.core.exceptions import ImproperlyConfigured
__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models') __all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models')
_app_list = None # Cache of installed apps. _app_list = None # Cache of installed apps.
_app_models = {} # Dictionary of models against app module name
def get_apps(): def get_apps():
"Returns a list of all installed modules that contain models." "Returns a list of all installed modules that contain models."
@ -33,11 +34,11 @@ def get_models(app_mod=None):
returns a list of all installed models. returns a list of all installed models.
""" """
if app_mod: if app_mod:
return getattr(app_mod, '_MODELS', ()) return _app_models.get(app_mod.__name__.split('.')[-2], ())
else: else:
model_list = [] model_list = []
for app_mod in get_apps(): for app_mod in get_apps():
model_list.extend(getattr(app_mod, '_MODELS', ())) model_list.extend(get_models(app_mod))
return model_list return model_list
def get_model(app_label, model_name): def get_model(app_label, model_name):
@ -51,13 +52,8 @@ def get_model(app_label, model_name):
model._meta.app_label == app_label: model._meta.app_label == app_label:
return model return model
def register_models(app_mod, *models): def register_models(app_label, *models):
""" """
Use this from an app's models.py module to register imported Model classes Register a set of models as belonging to an app.
as belonging to the app. e.g.:
register_models(sys.modules[__name__], Article, Reporter)
""" """
if not hasattr(app_mod, '_MODELS'): _app_models.setdefault(app_label, []).extend(models)
app_mod._MODELS = []
app_mod._MODELS.extend(models)

View File

@ -167,7 +167,7 @@ class Options:
if not hasattr(self, '_ordered_objects'): if not hasattr(self, '_ordered_objects'):
objects = [] objects = []
# TODO # TODO
#for klass in get_app(self.app_label)._MODELS: #for klass in get_models(get_app(self.app_label)):
# opts = klass._meta # opts = klass._meta
# if opts.order_with_respect_to and opts.order_with_respect_to.rel \ # if opts.order_with_respect_to and opts.order_with_respect_to.rel \
# and self == opts.order_with_respect_to.rel.to._meta: # and self == opts.order_with_respect_to.rel.to._meta:

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import os, re, sys, time, traceback import os, re, sys, time, traceback
import django.db.models
# doctest is included in the same package as this module, because this testing # doctest is included in the same package as this module, because this testing
# framework uses features only available in the Python 2.4 version of doctest, # framework uses features only available in the Python 2.4 version of doctest,
@ -142,7 +143,8 @@ class TestRunner:
# Run the API tests. # Run the API tests.
p = doctest.DocTestParser() p = doctest.DocTestParser()
test_namespace = dict([(m._meta.object_name, m) for m in mod._MODELS]) test_namespace = dict([(m._meta.object_name, m) \
for m in django.db.models.get_models(mod)])
dtest = p.get_doctest(mod.API_TESTS, test_namespace, model_name, None, None) dtest = p.get_doctest(mod.API_TESTS, test_namespace, model_name, None, None)
# Manually set verbose=False, because "-v" command-line parameter # Manually set verbose=False, because "-v" command-line parameter
# has side effects on doctest TestRunner class. # has side effects on doctest TestRunner class.