Skip to content

Commit

Permalink
Fixed #19401 -- Ensure that swappable model references are case insen…
Browse files Browse the repository at this point in the history
…sitive.

This is necessary because get_model() checks are case insensitive, and if the swapable check isn't, the
swappable logic gets tied up in knots with models that are partially swapped out.

Thanks to chris@cogdon.org for the report and extensive analysis, and Preston for his work on the draft patch.
  • Loading branch information
freakboy3742 committed Dec 20, 2012
1 parent 3989ce5 commit c04c03d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
19 changes: 16 additions & 3 deletions django/db/models/options.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -213,12 +213,25 @@ def _swapped(self):
""" """
Has this model been swapped out for another? If so, return the model Has this model been swapped out for another? If so, return the model
name of the replacement; otherwise, return None. name of the replacement; otherwise, return None.
For historical reasons, model name lookups using get_model() are
case insensitive, so we make sure we are case insensitive here.
""" """
if self.swappable: if self.swappable:
model_label = '%s.%s' % (self.app_label, self.object_name) model_label = '%s.%s' % (self.app_label, self.object_name.lower())
swapped_for = getattr(settings, self.swappable, None) swapped_for = getattr(settings, self.swappable, None)
if swapped_for not in (None, model_label): if swapped_for:
return swapped_for try:
swapped_label, swapped_object = swapped_for.split('.')
except ValueError:
# setting not in the format app_label.model_name
# raising ImproperlyConfigured here causes problems with
# test cleanup code - instead it is raised in get_user_model
# or as part of validation.
return swapped_for

if '%s.%s' % (swapped_label, swapped_object.lower()) not in (None, model_label):
return swapped_for
return None return None
swapped = property(_swapped) swapped = property(_swapped)


Expand Down
12 changes: 12 additions & 0 deletions tests/regressiontests/swappable_models/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings


from regressiontests.swappable_models.models import Article



class SwappableModelTests(TestCase): class SwappableModelTests(TestCase):
def setUp(self): def setUp(self):
Expand Down Expand Up @@ -44,3 +46,13 @@ def test_generated_data(self):
for ct in ContentType.objects.all()] for ct in ContentType.objects.all()]
self.assertIn(('swappable_models', 'alternatearticle'), apps_models) self.assertIn(('swappable_models', 'alternatearticle'), apps_models)
self.assertNotIn(('swappable_models', 'article'), apps_models) self.assertNotIn(('swappable_models', 'article'), apps_models)

@override_settings(TEST_ARTICLE_MODEL='swappable_models.article')
def test_case_insensitive(self):
"Model names are case insensitive. Check that model swapping honors this."
try:
Article.objects.all()
except AttributeError:
self.fail('Swappable model names should be case insensitive.')

self.assertIsNone(Article._meta.swapped)

0 comments on commit c04c03d

Please sign in to comment.