Add protection against mixed up ContentType tables

fix_request_path_info
Diederik van der Boor 2014-04-04 15:28:01 +02:00
parent acf1ddc086
commit e8d5ce6231
2 changed files with 10 additions and 1 deletions

View File

@ -7,6 +7,7 @@ Version 0.5.4 (in development)
* Fix ``.non_polymorphic()`` to returns a clone of the queryset, instead of effecting the existing queryset.
* Fix missing ``alters_data = True`` annotations on the overwritten ``save()`` methods.
* Fix infinite recursion bug in the admin with Django 1.6+
* Added detection of bad ``ContentType`` table data.
Version 0.5.3 (2013-09-17)

View File

@ -103,11 +103,19 @@ class PolymorphicModel(six.with_metaclass(PolymorphicModelBase, models.Model)):
# Note that model_class() can return None for stale content types;
# when the content type record still exists but no longer refers to an existing model.
try:
return ContentType.objects.get_for_id(self.polymorphic_ctype_id).model_class()
model = ContentType.objects.get_for_id(self.polymorphic_ctype_id).model_class()
except AttributeError:
# Django <1.6 workaround
return None
# Protect against bad imports (dumpdata without --natural) or other
# issues missing with the ContentType models.
if not issubclass(model, self.__class__):
raise RuntimeError("ContentType {0} for {1} #{2} does not point to a subclass!".format(
self.polymorphic_ctype_id, model, self.pk,
))
return model
def get_real_concrete_instance_class_id(self):
model_class = self.get_real_instance_class()
if model_class is None: