我有一個名爲「阻止」Django的辨兒童的QuerySet
class Block(models.Model):
zone = models.ForeignKey(Zone)
order = models.IntegerField()
weight = models.IntegerField()
block_type = models.CharField(max_length=32, blank=True)
class Meta:
ordering = ['order']
def __str__(self):
return "Block "+str(self.order)
的塊對象有孩子從它繼承模式。 ImageBlock,VideoBlock等...隨着項目的推進,我將增加更多
# Children of Block Object
class TextBlock(Block):
content = models.TextField(blank=True)
class ImageBlock(Block):
content = models.ImageField(blank=True)
class VideoBlock(Block):
content = models.FileField(blank=True)
我需要根據它們的順序對塊執行操作。如,
渲染TextBlock1 渲染ImageBlock1 渲染TextBlock2
我查詢了所有這些對象連同Block.objects.all()線的東西,然後遍歷它們。當我這樣做時,我該如何區分每個對象的哪些對象?
如:
blockset = Block.objects.all()
for block in blockset:
if (**some way of differentiating if it's a TextBlock**):
print("This is a text block!")
的我怎麼會去這樣做的任何想法?
非常感謝!
什麼''block_type''?你不使用它來確定類型嗎? – 2015-02-09 22:18:25
這可能比您需要的更多或更少,但您可能對django_model_utils pacakge感興趣,以便您可以執行諸如[this]之類的操作(https://django-model-utils.readthedocs.org/en/latest/managers.html #inheritancemanager)。 – 2015-02-09 22:39:53