Django: Cannot delete broken PolymorphicModel instance

1.1k Views Asked by At

I have this model setup in a Django project (hosted on Heroku) where I am using django-polymorphic:

class Product(PolymorphicModel):
    ...

class Book(Product):
    ...

The problem is, that I have managed to create a corrupted instance of both a Product and a Book. This happened when I had an instance of Product which I wanted to convert to an instance of Book and in a moment of insanity did a stupid thing like this:

> p
<Product: SomeTitle>
> b = Book()
> b.__dict__ = p.__dict__
> b.save()

Now, I know this was idiotic and it threw all kinds of errors. My problem is, that now I am stuck with an instance p which I cannot delete. When calling p.delete() I get the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-3-6a6f0dca8e5f> in <module>()
----> 1 p.delete()

~/.heroku/python/lib/python3.6/site-packages/django/db/models/base.py in delete(self, using, keep_parents)
    971
    972         collector = Collector(using=using)
--> 973         collector.collect([self], keep_parents=keep_parents)
    974         return collector.delete()
    975

~/.heroku/python/lib/python3.6/site-packages/django/db/models/deletion.py in collect(self, objs, source, nullable, collect_related, source_attr, reverse_dependency, keep_parents)
    220                     if self.can_fast_delete(sub_objs, from_field=field):
    221                         self.fast_deletes.append(sub_objs)
--> 222                     elif sub_objs:
    223                         field.remote_field.on_delete(self, field, sub_objs, self.using)
    224             for field in model._meta.private_fields:

~/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py in __bool__(self)
    252
    253     def __bool__(self):
--> 254         self._fetch_all()
    255         return bool(self._result_cache)
    256

~/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py in _fetch_all(self)
   1116     def _fetch_all(self):
   1117         if self._result_cache is None:
-> 1118             self._result_cache = list(self._iterable_class(self))
   1119         if self._prefetch_related_lookups and not self._prefetch_done:
   1120             self._prefetch_related_objects()

~/.heroku/python/lib/python3.6/site-packages/polymorphic/query.py in _polymorphic_iterator(self, base_iter)
     60                     break
     61
---> 62             real_results = self.queryset._get_real_instances(base_result_objects)
     63
     64             for o in real_results:

~/.heroku/python/lib/python3.6/site-packages/polymorphic/query.py in _get_real_instances(self, base_result_objects)
    360
    361                 else:
--> 362                     real_concrete_class = base_object.get_real_instance_class()
    363                     real_concrete_class_id = base_object.get_real_concrete_instance_class_id()
    364

~/.heroku/python/lib/python3.6/site-packages/polymorphic/models.py in get_real_instance_class(self)
    110         if model is not None \
    111                 and not issubclass(model, self.__class__) \
--> 112                 and not issubclass(model, self.__class__._meta.proxy_for_model):
    113             raise PolymorphicTypeInvalid("ContentType {0} for {1} #{2} does not point to a subclass!".format(
    114                 self.polymorphic_ctype_id, model, self.pk,

TypeError: issubclass() arg 2 must be a class or tuple of classes

Furthermore, if I try and do

> p = Product.objects.get(title='SomeTitle')
> p
<Product: SomeTitle>
> b = Book.objects.get(title=p.title)

I get the same error. It also appears if I create any QuerySet that contains this Book instance, which is kind of a problem.

My question is: how do I circumvent this issue and delete the corrupted instance?

0

There are 0 best solutions below