我的项目使用三种图像模型。模型是
STAGE = [
('Initial', 'Initial'),
('Observation','Observation'),
('Initial', 'Initial')
('Progress','Progress'),
('Final', 'Final'),
('Post treatment','Post treatment'),
]
class Xray(models.Model):
date = models.DateTime()
desc = models.CharField(choices=XRAY, max_length=15)
stage = models.CharField(choices=STAGE, max_length=20)
image = models.ImageField(...)
class External(models.Model):
date = models.DateTime()
desc = models.CharField(choices=EXTERNAL, max_length=15)
stage = models.CharField(choices=STAGE, max_length=20)
image = models.ImageField(...)
class Internal(models.Model):
date = models.DateTime()
desc = models.CharField(choices=INTERNAL, max_length=15)
stage = models.CharField(choices=STAGE, max_length=20)
image = models.ImageField(...)
我希望能够在我的模板上显示这三个模型,如下所示:
Date:Stage
All pictures that have date = Date and stage=Stage
如何对它们进行分组以便我能够对此进行分组?你认为如果舞台本身就是一个模特并且在每个图像模式中成为一个外键并且日期是舞台模型的一个领域会更好吗?
class Internal(models.Model):
desc = models.CharField(choices=INTERNAL, max_length=15)
stage = models.CharField(choices=STAGE, max_length=20)
image = models.ImageField(...)
stage = models.ForeignKey(Stage)
class Stage(models.Model):
title = models.CharField(choices=STAGE, max_lenght=20)
date = models.DateTime()
请注意,一张图片只能有一个日期并且处于一个阶段(因此,OneToOne字段可能更好吗?)
答案 0 :(得分:1)
您可以将Image
建模为Concrete Base Class
class Image(models.Model):
date = models.DateTime()
stage = models.CharField(choices=STAGE, max_length=20)
image = models.ImageField(...)
class Xray(Image):
desc = models.CharField(choices=XRAY, max_length=15)
....
然后,您可以使用以下正常语法查询所有三种Image
对象:
Image.objects.filter(date=datetime.date(2014, 1, 3), stage='Initial')