Added AppConfig.get_models().
This commit is contained in:
parent
98b52ae201
commit
7b88a96553
|
@ -126,6 +126,32 @@ class AppConfig(object):
|
||||||
raise LookupError(
|
raise LookupError(
|
||||||
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
|
"App '%s' doesn't have a '%s' model." % (self.label, model_name))
|
||||||
|
|
||||||
|
def get_models(self, include_auto_created=False,
|
||||||
|
include_deferred=False, include_swapped=False):
|
||||||
|
"""
|
||||||
|
Returns an iterable of models.
|
||||||
|
|
||||||
|
By default, the following models aren't included:
|
||||||
|
|
||||||
|
- auto-created models for many-to-many relations without
|
||||||
|
an explicit intermediate table,
|
||||||
|
- models created to satisfy deferred attribute queries,
|
||||||
|
- models that have been swapped out.
|
||||||
|
|
||||||
|
Set the corresponding keyword argument to True to include such models.
|
||||||
|
Keyword arguments aren't documented; they're a private API.
|
||||||
|
|
||||||
|
This method assumes that apps.populate_models() has run.
|
||||||
|
"""
|
||||||
|
for model in self.models.values():
|
||||||
|
if model._deferred and not include_deferred:
|
||||||
|
continue
|
||||||
|
if model._meta.auto_created and not include_auto_created:
|
||||||
|
continue
|
||||||
|
if model._meta.swapped and not include_swapped:
|
||||||
|
continue
|
||||||
|
yield model
|
||||||
|
|
||||||
def import_models(self, all_models):
|
def import_models(self, all_models):
|
||||||
# Dictionary of models for this app, primarily maintained in the
|
# Dictionary of models for this app, primarily maintained in the
|
||||||
# 'all_models' attribute of the Apps this AppConfig is attached to.
|
# 'all_models' attribute of the Apps this AppConfig is attached to.
|
||||||
|
|
|
@ -74,14 +74,12 @@ def create_permissions(app_config, verbosity=22, interactive=True, db=DEFAULT_DB
|
||||||
|
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
|
||||||
app_models = apps.get_models(app_config.models_module)
|
|
||||||
|
|
||||||
# This will hold the permissions we're looking for as
|
# This will hold the permissions we're looking for as
|
||||||
# (content_type, (codename, name))
|
# (content_type, (codename, name))
|
||||||
searched_perms = list()
|
searched_perms = list()
|
||||||
# The codenames and ctypes that should exist.
|
# The codenames and ctypes that should exist.
|
||||||
ctypes = set()
|
ctypes = set()
|
||||||
for klass in app_models:
|
for klass in app_config.get_models():
|
||||||
# Force looking up the content types in the current database
|
# Force looking up the content types in the current database
|
||||||
# before creating foreign keys to them.
|
# before creating foreign keys to them.
|
||||||
ctype = ContentType.objects.db_manager(db).get_for_model(klass)
|
ctype = ContentType.objects.db_manager(db).get_for_model(klass)
|
||||||
|
|
|
@ -23,15 +23,15 @@ def update_contenttypes(app_config, verbosity=2, interactive=True, db=DEFAULT_DB
|
||||||
return
|
return
|
||||||
|
|
||||||
ContentType.objects.clear_cache()
|
ContentType.objects.clear_cache()
|
||||||
app_models = apps.get_models(app_config.models_module)
|
|
||||||
if not app_models:
|
app_label = app_config.label
|
||||||
return
|
|
||||||
# They all have the same app_label, get the first one.
|
|
||||||
app_label = app_models[0]._meta.app_label
|
|
||||||
app_models = dict(
|
app_models = dict(
|
||||||
(model._meta.model_name, model)
|
(model._meta.model_name, model)
|
||||||
for model in app_models
|
for model in app_config.get_models())
|
||||||
)
|
|
||||||
|
if not app_models:
|
||||||
|
return
|
||||||
|
|
||||||
# Get all the content types
|
# Get all the content types
|
||||||
content_types = dict(
|
content_types = dict(
|
||||||
|
|
|
@ -169,7 +169,7 @@ def sort_dependencies(app_list):
|
||||||
models = set()
|
models = set()
|
||||||
for app_config, model_list in app_list:
|
for app_config, model_list in app_list:
|
||||||
if model_list is None:
|
if model_list is None:
|
||||||
model_list = apps.get_models(app_config.models_module)
|
model_list = app_config.get_models()
|
||||||
|
|
||||||
for model in model_list:
|
for model in model_list:
|
||||||
models.add(model)
|
models.add(model)
|
||||||
|
|
|
@ -23,6 +23,6 @@ class Command(AppCommand):
|
||||||
if app_config.models_module is None:
|
if app_config.models_module is None:
|
||||||
return
|
return
|
||||||
connection = connections[options.get('database')]
|
connection = connections[options.get('database')]
|
||||||
models = apps.get_models(app_config.models_module, include_auto_created=True)
|
models = app_config.get_models(include_auto_created=True)
|
||||||
statements = connection.ops.sequence_reset_sql(self.style, models)
|
statements = connection.ops.sequence_reset_sql(self.style, models)
|
||||||
return '\n'.join(statements)
|
return '\n'.join(statements)
|
||||||
|
|
|
@ -156,6 +156,10 @@ Read-only attributes
|
||||||
Methods
|
Methods
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
.. method:: AppConfig.get_models()
|
||||||
|
|
||||||
|
Returns an iterable of :class:`~django.db.models.Model` classes.
|
||||||
|
|
||||||
.. method:: AppConfig.get_model(model_name)
|
.. method:: AppConfig.get_model(model_name)
|
||||||
|
|
||||||
Returns the :class:`~django.db.models.Model` with the given
|
Returns the :class:`~django.db.models.Model` with the given
|
||||||
|
|
Loading…
Reference in New Issue