Add protection against mixed up ContentType tables

This commit is contained in:
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
+9 -1
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: