Fixed #27360 -- Added app or ModelAdmin details for AreadyRegistered exceptions.
This commit is contained in:
parent
981dd6dd71
commit
d4df5e1b0b
|
@ -1,3 +1,4 @@
|
||||||
|
import re
|
||||||
from functools import update_wrapper
|
from functools import update_wrapper
|
||||||
from weakref import WeakSet
|
from weakref import WeakSet
|
||||||
|
|
||||||
|
@ -106,7 +107,14 @@ class AdminSite:
|
||||||
)
|
)
|
||||||
|
|
||||||
if model in self._registry:
|
if model in self._registry:
|
||||||
raise AlreadyRegistered('The model %s is already registered' % model.__name__)
|
registered_admin = str(self._registry[model])
|
||||||
|
msg = 'The model %s is already registered ' % model.__name__
|
||||||
|
if registered_admin.endswith('.ModelAdmin'):
|
||||||
|
# Most likely registered without a ModelAdmin subclass.
|
||||||
|
msg += 'in app %r.' % re.sub(r'\.ModelAdmin$', '', registered_admin)
|
||||||
|
else:
|
||||||
|
msg += 'with %r.' % registered_admin
|
||||||
|
raise AlreadyRegistered(msg)
|
||||||
|
|
||||||
# Ignore the registration if the model has been
|
# Ignore the registration if the model has been
|
||||||
# swapped out.
|
# swapped out.
|
||||||
|
|
|
@ -30,10 +30,19 @@ class TestRegistration(SimpleTestCase):
|
||||||
|
|
||||||
def test_prevent_double_registration(self):
|
def test_prevent_double_registration(self):
|
||||||
self.site.register(Person)
|
self.site.register(Person)
|
||||||
msg = 'The model Person is already registered'
|
msg = "The model Person is already registered in app 'admin_registration'."
|
||||||
with self.assertRaisesMessage(admin.sites.AlreadyRegistered, msg):
|
with self.assertRaisesMessage(admin.sites.AlreadyRegistered, msg):
|
||||||
self.site.register(Person)
|
self.site.register(Person)
|
||||||
|
|
||||||
|
def test_prevent_double_registration_for_custom_admin(self):
|
||||||
|
class PersonAdmin(admin.ModelAdmin):
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.site.register(Person, PersonAdmin)
|
||||||
|
msg = "The model Person is already registered with 'admin_registration.PersonAdmin'."
|
||||||
|
with self.assertRaisesMessage(admin.sites.AlreadyRegistered, msg):
|
||||||
|
self.site.register(Person, PersonAdmin)
|
||||||
|
|
||||||
def test_registration_with_star_star_options(self):
|
def test_registration_with_star_star_options(self):
|
||||||
self.site.register(Person, search_fields=['name'])
|
self.site.register(Person, search_fields=['name'])
|
||||||
self.assertEqual(self.site._registry[Person].search_fields, ['name'])
|
self.assertEqual(self.site._registry[Person].search_fields, ['name'])
|
||||||
|
|
Loading…
Reference in New Issue