For django-model-utils, how can `select_subclasses()` be applied to fields of an object?

旧巷老猫 提交于 2019-12-24 15:24:10

问题


I have some models like this:

class Container(models.Model):
    pass

class Parent(models.Model):
    container = models.ForeignKey(Container, related_name='items')
    pass

class Child(Parent):
    pass

class RedHeadedStepChild(Parent):
    pass

Is it possible to use select_subclasses() to prefetch fields in the container? I want to do something like this:

qs = Container.objects.all().prefetch_related('items')\
     .select_subclasses() # <---

So that the items related field of each Container is retrieved in each of its respective subclass types.


回答1:


What about:

Container.objects.prefetch_related(
    Prefetch('items', Parent.objects.select_subclasses())
)


来源:https://stackoverflow.com/questions/29014085/for-django-model-utils-how-can-select-subclasses-be-applied-to-fields-of-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!