Fixed #5968 -- Allowed (un-)registering with databrowse several models at once.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17405 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Aymeric Augustin 2012-01-28 20:54:48 +00:00
parent 4a3aceed3f
commit 1cc800783c
3 changed files with 17 additions and 11 deletions

View File

@ -73,7 +73,7 @@ class DatabrowseSite(object):
self.registry = {} # model_class -> databrowse_class self.registry = {} # model_class -> databrowse_class
self.root_url = None self.root_url = None
def register(self, model_or_iterable, databrowse_class=None, **options): def register(self, *model_list, **options):
""" """
Registers the given model(s) with the given databrowse site. Registers the given model(s) with the given databrowse site.
@ -84,23 +84,19 @@ class DatabrowseSite(object):
If a model is already registered, this will raise AlreadyRegistered. If a model is already registered, this will raise AlreadyRegistered.
""" """
databrowse_class = databrowse_class or DefaultModelDatabrowse databrowse_class = options.pop('databrowse_class', DefaultModelDatabrowse)
if issubclass(model_or_iterable, models.Model): for model in model_list:
model_or_iterable = [model_or_iterable]
for model in model_or_iterable:
if model in self.registry: if model in self.registry:
raise AlreadyRegistered('The model %s is already registered' % model.__name__) raise AlreadyRegistered('The model %s is already registered' % model.__name__)
self.registry[model] = databrowse_class self.registry[model] = databrowse_class
def unregister(self, model_or_iterable): def unregister(self, *model_list):
""" """
Unregisters the given model(s). Unregisters the given model(s).
If a model isn't already registered, this will raise NotRegistered. If a model isn't already registered, this will raise NotRegistered.
""" """
if issubclass(model_or_iterable, models.Model): for model in model_list:
model_or_iterable = [model_or_iterable]
for model in model_or_iterable:
if model not in self.registry: if model not in self.registry:
raise NotRegistered('The model %s is not registered' % model.__name__) raise NotRegistered('The model %s is not registered' % model.__name__)
del self.registry[model] del self.registry[model]

View File

@ -33,13 +33,18 @@ How to use Databrowse
2. Register a number of models with the Databrowse site:: 2. Register a number of models with the Databrowse site::
from django.contrib import databrowse from django.contrib import databrowse
from myapp.models import SomeModel, SomeOtherModel from myapp.models import SomeModel, SomeOtherModel, YetAnotherModel
databrowse.site.register(SomeModel) databrowse.site.register(SomeModel)
databrowse.site.register(SomeOtherModel) databrowse.site.register(SomeOtherModel, YetAnotherModel)
Note that you should register the model *classes*, not instances. Note that you should register the model *classes*, not instances.
.. versionchanged:: 1.4
Since Django 1.4, it is possible to register several models in the same
call to :func:`~databrowse.site.register`.
It doesn't matter where you put this, as long as it gets executed at some It doesn't matter where you put this, as long as it gets executed at some
point. A good place for it is in your :doc:`URLconf file point. A good place for it is in your :doc:`URLconf file
</topics/http/urls>` (``urls.py``). </topics/http/urls>` (``urls.py``).

View File

@ -4,9 +4,13 @@ import shutil
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
import warnings
from django import contrib from django import contrib
# databrowse is deprecated, but we still want to run its tests
warnings.filterwarnings('ignore', "The Databrowse contrib app is deprecated",
PendingDeprecationWarning, 'django.contrib.databrowse')
CONTRIB_DIR_NAME = 'django.contrib' CONTRIB_DIR_NAME = 'django.contrib'
MODEL_TESTS_DIR_NAME = 'modeltests' MODEL_TESTS_DIR_NAME = 'modeltests'
@ -34,6 +38,7 @@ ALWAYS_INSTALLED_APPS = [
'django.contrib.comments', 'django.contrib.comments',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.admindocs', 'django.contrib.admindocs',
'django.contrib.databrowse',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django.contrib.humanize', 'django.contrib.humanize',
'regressiontests.staticfiles_tests', 'regressiontests.staticfiles_tests',