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.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.
@ -84,23 +84,19 @@ class DatabrowseSite(object):
If a model is already registered, this will raise AlreadyRegistered.
"""
databrowse_class = databrowse_class or DefaultModelDatabrowse
if issubclass(model_or_iterable, models.Model):
model_or_iterable = [model_or_iterable]
for model in model_or_iterable:
databrowse_class = options.pop('databrowse_class', DefaultModelDatabrowse)
for model in model_list:
if model in self.registry:
raise AlreadyRegistered('The model %s is already registered' % model.__name__)
self.registry[model] = databrowse_class
def unregister(self, model_or_iterable):
def unregister(self, *model_list):
"""
Unregisters the given model(s).
If a model isn't already registered, this will raise NotRegistered.
"""
if issubclass(model_or_iterable, models.Model):
model_or_iterable = [model_or_iterable]
for model in model_or_iterable:
for model in model_list:
if model not in self.registry:
raise NotRegistered('The model %s is not registered' % model.__name__)
del self.registry[model]

View File

@ -33,13 +33,18 @@ How to use Databrowse
2. Register a number of models with the Databrowse site::
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(SomeOtherModel)
databrowse.site.register(SomeOtherModel, YetAnotherModel)
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
point. A good place for it is in your :doc:`URLconf file
</topics/http/urls>` (``urls.py``).

View File

@ -4,9 +4,13 @@ import shutil
import subprocess
import sys
import tempfile
import warnings
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'
MODEL_TESTS_DIR_NAME = 'modeltests'
@ -34,6 +38,7 @@ ALWAYS_INSTALLED_APPS = [
'django.contrib.comments',
'django.contrib.admin',
'django.contrib.admindocs',
'django.contrib.databrowse',
'django.contrib.staticfiles',
'django.contrib.humanize',
'regressiontests.staticfiles_tests',