我在使用django-polymorphic的 Django 项目(托管在 Heroku 上)中有这个模型设置:
class Product(PolymorphicModel):
...
class Book(Product):
...
问题是,我设法创建了 aProduct
和 a的损坏实例Book
。当我有一个实例Product
,我想将其转换为一个实例时,就会发生这种情况,Book
并且在精神错乱的时刻做了这样一件愚蠢的事情:
> p
<Product: SomeTitle>
> b = Book()
> b.__dict__ = p.__dict__
> b.save()
现在,我知道这很愚蠢,并且引发了各种错误。我的问题是,现在我遇到了一个p
无法删除的实例。调用时p.delete()
出现以下错误:
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
此外,如果我尝试做
> p = Product.objects.get(title='SomeTitle')
> p
<Product: SomeTitle>
> b = Book.objects.get(title=p.title)
我犯了同样的错误。如果我创建包含此Book
实例的任何 QuerySet,它也会出现,这是一个问题。
我的问题是:如何规避此问题并删除损坏的实例?