Find object in child class from object in parent class in django

后端 未结 6 1663
春和景丽
春和景丽 2021-01-02 17:46

Let\'s say I have a parent class (ThingsThatMigrate) and two children (Coconut and Swallow). Now let\'s say I have a ThingsThatMigrate object. How can I determine if it is

6条回答
  •  有刺的猬
    2021-01-02 18:07

    As DrMeer suggested, I highly recommend django-model-utils (hosted on bitbucket now though). I'm not sure it's convincing enough though.
    Let a code example prove it:

    >>> ThingsThatMigrate.objects.all().select_subclasses()
    Coconut, Coconut, Swallow, Coconut, ThingsThatMigrate
    

    It takes one line, objects = InheritanceManager() in your parent model.

提交回复
热议问题