我有三个型号,
MTI
=材料信息MTD
=材料说明MTIIMAGE
=每种颜色和尺寸都有图片我正在尝试创建一个返回不同颜色的查询,但是我得到的颜色结果并不是唯一的:
mymti[0].mtd_set.all()
[mtd: 1234567 - Medium black, mtd: 1122334455 - Medium pink, mtd: 12345678
- Large black]
如何从此查询中获取不同的颜色,以便结果为
[mtd:1234567 - 中黑,mtd:1122334455 - 中等粉红色]
class mti(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
item = models.ForeignKey(item)
country = models.ForeignKey(country)
dept = models.ForeignKey(dept)
fabric = models.ForeignKey(fabric)
sesason = models.ForeignKey(season)
sale = models.FloatField()
endUser = models.FloatField()
description = models.TextField(max_length=1000,blank=True,null=True)
year = models.CharField(max_length=255,default=strftime("%Y", gmtime()))
front_page = models.BooleanField(verbose_name="Front Page",default=True)
active = models.BooleanField(default=True)
stamp = models.DateTimeField(auto_now=True)
def __unicode__(self):
return u'%s - %s' % (self.name, self.description)
class Meta:
verbose_name = "MTI"
verbose_name_plural = verbose_name
class mtd(models.Model):
id = models.AutoField(primary_key=True)
barcode = models.CharField(max_length=255)
mti = models.ForeignKey(mti)
size = models.ForeignKey(size)
color = models.ForeignKey(color)
weight = models.FloatField()
class Meta:
verbose_name = "MTD"
verbose_name_plural = verbose_name
def __unicode__(self):
return u'%s - %s %s' % (self.barcode, self.size.name,self.color.name)
class mtiimage(models.Model):
id = models.AutoField(primary_key=True)
color = models.ForeignKey(color)
mtd= models.ForeignKey(mtd)
mti=models.ForeignKey(mti)
pic1 = models.ImageField(verbose_name="Product 1 Image",upload_to='product')
pic2 = models.ImageField(verbose_name="Product 2 Image",upload_to='product')
pic3 = models.ImageField(verbose_name="Product 3 Image",upload_to='product')
stamp = models.DateTimeField(auto_now=True)
def __unicode__(self):
return u'%s - %s' % (self.mtd.barcode, self.color.name)
class Meta:
verbose_name = "MTI Pic"
verbose_name_plural = verbose_name
我的观点是
mydept = dept.objects.get(pk=deptid)
mymti = mti.objects.filter(dept=mydept).select_related()
我的模板
{% for i in mymti %}
{% for m in i.mtd_set.all %}{{m.mtiimage_set.all.0.pic1.url}}{{m.mtiimage_set.all.0.color.name|lower}}{% endfor %}
{% endfor %}
答案 0 :(得分:0)
我通过将mtiimage外键放到mti来重新设计我的模型来解决这个问题 这是我可以直接查询
for m in mti.mtiimage_set.all() print n.color.name