我有一个名为" Block"
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)
Block对象具有从中继承的子对象。 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!")
我知道如何做到这一点吗?
非常感谢!
答案 0 :(得分:3)
如果您不知道要获得的课程名称,可以在父模型上使用Content Types。例如:
(未经测试)
from django.contrib.contenttypes.models import ContentType
class Block(models.Model):
zone = models.ForeignKey(Zone)
order = models.IntegerField()
weight = models.IntegerField()
block_type = models.ForeignKey(ContentType, editable=False)
def save(self, *args, **kwargs):
if not self.id:
self.block_type = self._block_type()
super(Block, self).save(*args, **kwargs)
def __block_type(self):
return ContentType.objects.get_for_model(type(self))
def cast(self):
return self.block_type.get_object_for_this_type(pk=self.pk)
class Meta:
abstract = True
另请注意抽象基类,这意味着不会在数据库中创建模型。抽象字段将被添加到子类的那些字段,即
class TextBlock(Block):
content = models.TextField(blank=True)
但是,您无法在此示例中查询抽象基类。如果这是你想要做的,那么在你的基类上添加一个简单的查找。
TEXT = 'TXT'
IMAGE = 'IMG'
VIDEO = 'VID'
BLOCK_CHOICES = (
(TEXT, 'Text Block'),
(IMAGE, 'Image Block'),
(VIDEO, 'Video Block'),
)
class Block(models.Model):
zone = models.ForeignKey(Zone)
order = models.IntegerField()
weight = models.IntegerField()
block_type = models.CharField(max_length=3, choices=BLOCK_CHOICES)
然后查询:Block.objects.filter(block_type='Text Block')
或者在你的例子中:
blockset = Block.objects.all()
for block in blockset:
if block.block_type == "Text Block":
print("This is a text block!")