1

这是我第一次尝试“PolymorphicModel”,也许我做错了什么,或者我正在尝试做一些不受支持的事情。有没有办法解决这个问题?

我的简化模型:

from django.polymorphic import PolymorphicModel


class Question(PolymorphicModel):
    description = models.TextField(default='')

    compound_question = models.ForeignKey('CompoundQuestion',
                                          on_delete=models.CASCADE,
                                          blank=True,
                                          null=True,
                                          related_name='sub_questions')

    bonus_question = models.OneToOneField('BonusQuestion',
                                          on_delete=models.CASCADE,
                                          blank=True,
                                          null=True,
                                          related_name='question')


class SimpleQuestion(Question):
    pass


class CompoundQuestion(Question):
    pass


class BonusQuestion(Question):
    pass

如果我创建必要的实例,

compound_q = CompoundQuestion()
compound_q.save()
simple_q2 = SimpleQuestion(compound_question=compound_q)
simple_q2.save()
bonus_q = BonusQuestion(compound_question=compound_q)
bonus_q.save()
simple_q1 = SimpleQuestion(bonus_question=bonus_q)
simple_q1.save()

所以最后我得到:

compound_q.sub_questions.all()
>>> <PolymorphicQuerySet [<BonusQuestion: BonusQuestion object (5)>, <SimpleQuestion: SimpleQuestion object (8)>]>

完成后,compound_q.delete() 我得到:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/base.py", line 936, in delete
    collector.collect([self], keep_parents=keep_parents)
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/deletion.py", line 245, in collect
    field.remote_field.on_delete(self, field, sub_objs, self.using)
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/deletion.py", line 17, in CASCADE
    source_attr=field.name, nullable=field.null)
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/deletion.py", line 226, in collect
    sub_objs = self.related_objects(related, batch)
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/deletion.py", line 257, in related_objects
    **{"%s__in" % related.field.name: objs}
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/query.py", line 904, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File ".../Python/Django/env/lib/python3.6/site-packages/polymorphic/query.py", line 173, in _filter_or_exclude
    negate, *(list(q_objects) + additional_args), **kwargs
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/query.py", line 923, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1340, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1371, in _add_q
    check_filterable=check_filterable,
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1274, in build_filter
    self.check_related_objects(join_info.final_field, value, join_info.opts)
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1115, in check_related_objects
    self.check_query_object_type(v, opts, field)
  File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1095, in check_query_object_type
    (value, opts.object_name))
ValueError: Cannot query "SimpleQuestion object (8)": Must be "BonusQuestion" instance.

我的模型有问题吗?其他一切似乎都按预期工作。谢谢阅读 :)

4

1 回答 1

2

这似乎是django-polymorphic 包中的一个已知错误。这里有一个建议的解决方法来为你的班级添加一个香草Manager并将这个经理设置为默认值

class Question(PolymorphicModel):

    non_polymorphic = models.Manager()

    class Meta
        base_manager_name = 'non_polymorphic'
于 2019-12-30T04:49:02.287 回答