Django access to subclasses items from abstract class

前端 未结 2 1747
眼角桃花
眼角桃花 2021-02-16 00:05
class Animal(models.Model):
    ....
    class Meta:
        abstract = True

class Cat(models.Model, Animal):
    ...

class Dog(models.Model, Animal):
    ....
         


        
2条回答
  •  [愿得一人]
    2021-02-16 00:21

    You might be interested in django_polymorphic

    Example from project docs:

    When we store models that inherit from a Project model...

    >>> Project.objects.create(topic="Department Party")
    >>> ArtProject.objects.create(topic="Painting with Tim", artist="T. Turner")
    >>> ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
    

    ...and want to retrieve all our projects, the subclassed models are returned:

    >>> Project.objects.all()
    [ ,
      ,
       ]
    

提交回复
热议问题