Ticket #26086: model_empty_list_registration_unregistration.diff

File model_empty_list_registration_unregistration.diff, 2.5 KB (added by anabelensc, 9 years ago)
  • django/contrib/admin/sites.py

    diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py
    index 77da4c0..ca4a7e4 100644
    a b class AdminSite(object):  
    7474        admin options). If keyword arguments are given -- e.g., list_display --
    7575        they'll be applied as options to the admin class.
    7676
     77        If a model is empty, this will raise ValueError.
     78
    7779        If a model is already registered, this will raise AlreadyRegistered.
    7880
    7981        If a model is abstract, this will raise ImproperlyConfigured.
    8082        """
    8183        if not admin_class:
    8284            admin_class = ModelAdmin
    83 
     85        if not model_or_iterable:
     86            raise ValueError('At least one model must be passed to register.')
    8487        if isinstance(model_or_iterable, ModelBase):
    8588            model_or_iterable = [model_or_iterable]
    8689        for model in model_or_iterable:
    class AdminSite(object):  
    113116    def unregister(self, model_or_iterable):
    114117        """
    115118        Unregisters the given model(s).
     119 
     120        If a model is empty, this will raise ValueError.
    116121
    117122        If a model isn't already registered, this will raise NotRegistered.
    118123        """
     124        if not model_or_iterable:
     125            raise ValueError('At least one model must be passed to unregister.')
    119126        if isinstance(model_or_iterable, ModelBase):
    120127            model_or_iterable = [model_or_iterable]
    121128        for model in model_or_iterable:
  • tests/admin_registration/tests.py

    diff --git a/tests/admin_registration/tests.py b/tests/admin_registration/tests.py
    index 202fed8..168cbf8 100644
    a b class TestRegistration(SimpleTestCase):  
    7070        """
    7171        self.assertRaises(ImproperlyConfigured, self.site.register, Location)
    7272
     73    def test_empty_models_list_registration(self):
     74        with self.assertRaisesMessage(ValueError, 'At least one model must be passed to register.'):self.site.register(())
     75
    7376    def test_is_registered_model(self):
    7477        "Checks for registered models should return true."
    7578        self.site.register(Person)
    class TestRegistration(SimpleTestCase):  
    7982        "Checks for unregistered models should return false."
    8083        self.assertFalse(self.site.is_registered(Person))
    8184
     85    def test_empty_models_list_unregistration(self):
     86        with self.assertRaisesMessage(ValueError, 'At least one model must be passed to unregister.'):self.site.unregister(())
     87
    8288
    8389class TestRegistrationDecorator(SimpleTestCase):
    8490    """
Back to Top