Ticket #5968: 5968-3.diff
File 5968-3.diff, 5.6 KB (added by , 13 years ago) |
---|
-
django/contrib/databrowse/sites.py
diff --git a/django/contrib/databrowse/sites.py b/django/contrib/databrowse/sites.py index 172ca52..eed8071 100644
a b 1 1 from django import http 2 2 from django.db import models 3 from django.db.models.base import ModelBase 3 4 from django.contrib.databrowse.datastructures import EasyModel 4 5 from django.shortcuts import render_to_response 5 6 from django.utils.safestring import mark_safe … … class DatabrowseSite(object): 85 86 If a model is already registered, this will raise AlreadyRegistered. 86 87 """ 87 88 databrowse_class = databrowse_class or DefaultModelDatabrowse 88 if is subclass(model_or_iterable, models.Model):89 if isinstance(model_or_iterable, models.base.ModelBase): 89 90 model_or_iterable = [model_or_iterable] 90 91 for model in model_or_iterable: 91 92 if model in self.registry: … … class DatabrowseSite(object): 98 99 99 100 If a model isn't already registered, this will raise NotRegistered. 100 101 """ 101 if is subclass(model_or_iterable, models.Model):102 if isinstance(model_or_iterable, models.base.ModelBase): 102 103 model_or_iterable = [model_or_iterable] 103 104 for model in model_or_iterable: 104 105 if model not in self.registry: -
docs/ref/contrib/databrowse.txt
diff --git a/docs/ref/contrib/databrowse.txt b/docs/ref/contrib/databrowse.txt index 33c8228..fac541b 100644
a b How to use Databrowse 39 39 2. Register a number of models with the Databrowse site:: 40 40 41 41 from django.contrib import databrowse 42 from myapp.models import SomeModel, SomeOtherModel 42 from myapp.models import SomeModel, SomeOtherModel, YetAnotherModel 43 43 44 44 databrowse.site.register(SomeModel) 45 databrowse.site.register( SomeOtherModel)45 databrowse.site.register([SomeOtherModel, YetAnotherModel]) 46 46 47 47 Note that you should register the model *classes*, not instances. 48 48 49 The register function accepts an individual model class or a list of classes. 50 49 51 It doesn't matter where you put this, as long as it gets executed at some 50 52 point. A good place for it is in your :doc:`URLconf file 51 53 </topics/http/urls>` (``urls.py``). -
new file tests/regressiontests/bug5968/models.py
diff --git a/tests/regressiontests/bug5968/__init__.py b/tests/regressiontests/bug5968/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/regressiontests/bug5968/models.py b/tests/regressiontests/bug5968/models.py new file mode 100644 index 0000000..3788b04
- + 1 from django.db import models 2 3 class SomeModel(models.Model): 4 some_field = models.CharField(max_length=50) 5 6 def __unicode__(self): 7 return self.some_field 8 9 class SomeOtherModel(models.Model): 10 some_other_field = models.CharField(max_length=50) 11 12 def __unicode__(self): 13 return self.some_other_field 14 15 class YetAnotherModel(models.Model): 16 yet_another_field = models.CharField(max_length=50) 17 18 def __unicode__(self): 19 return self.yet_another_field -
new file tests/regressiontests/bug5968/tests.py
diff --git a/tests/regressiontests/bug5968/tests.py b/tests/regressiontests/bug5968/tests.py new file mode 100644 index 0000000..cc796a0
- + 1 from django.contrib import databrowse 2 from django.test import TestCase 3 4 from models import SomeModel, SomeOtherModel, YetAnotherModel 5 6 class Bug5968Test(TestCase): 7 """ 8 Test for bug #5968 - allow an iterable containing models to be passed to 9 databrowse.site.register/unregister. 10 11 """ 12 13 def setUp(self): 14 sm1 = SomeModel.objects.create(some_field="Some Data 1") 15 sm2 = SomeModel.objects.create(some_field="Some Data 2") 16 som1 = SomeOtherModel.objects.create(some_other_field="Some Other Data 1") 17 som2 = SomeOtherModel.objects.create(some_other_field="Some Other Data 2") 18 yam1 = YetAnotherModel.objects.create(yet_another_field="Yet Some Other Data 1") 19 yam2 = YetAnotherModel.objects.create(yet_another_field="Yet Some Other Data 2") 20 21 def test_databrowse_registration(self): 22 databrowse.site.register(SomeModel) 23 self.assertEqual(SomeModel in databrowse.site.registry, True) 24 databrowse.site.register([SomeOtherModel, YetAnotherModel]) 25 self.assertEqual(SomeOtherModel in databrowse.site.registry and 26 YetAnotherModel in databrowse.site.registry, True) 27 28 self.assertRaisesMessage( 29 databrowse.sites.AlreadyRegistered, 30 'The model SomeModel is already registered', 31 databrowse.site.register, [SomeModel, SomeOtherModel] 32 ) 33 34 databrowse.site.unregister(SomeOtherModel) 35 self.assertEqual(SomeOtherModel not in databrowse.site.registry, True) 36 databrowse.site.unregister([SomeModel, YetAnotherModel]) 37 self.assertEqual(SomeModel not in databrowse.site.registry and 38 YetAnotherModel not in databrowse.site.registry, True) 39 40 self.assertRaisesMessage( 41 databrowse.sites.NotRegistered, 42 'The model SomeModel is not registered', 43 databrowse.site.unregister, [SomeModel, SomeOtherModel] 44 ) 45 46 self.assertRaisesMessage( 47 databrowse.sites.AlreadyRegistered, 48 'The model SomeModel is already registered', 49 databrowse.site.register, [SomeModel, SomeModel] 50 ) 51 52 databrowse.site.unregister(SomeModel) 53 databrowse.site.register([SomeModel]) 54 self.assertEqual(SomeModel in databrowse.site.registry, True) 55