多域不同值

发布于 2024-11-16 01:07:02 字数 2340 浏览 3 评论 0原文

我有一个名为 mti(材料信息)的模型,其中有一个 MTD(材料描述)列表,并且在每个材料描述描述中都有一个尺寸和颜色

我有一个 MTI id 我想获取所有不同的颜色值列表 mymti = mti.objects.get(pk=1)

我如何从 MTI 获取颜色列表 mymti.mtd.color.name ?

class color(models.Model):
id = models.AutoField(primary_key=True)
name= models.CharField(max_length=255)
stamp= models.DateTimeField(auto_now=True)
def __unicode__(self):
    return self.name
class mtd(models.Model):
id = models.AutoField(primary_key=True)
barcode = models.CharField(max_length=255)
#mti = models.ManyToManyField(mti)
size = models.ForeignKey(size)
color = models.ForeignKey(color)
weight = models.FloatField()
def __unicode__(self):
    return u'%s - %s %s' % (self.barcode,self.color.name,self.size.name)
class Meta:
    verbose_name = "MTD"
    verbose_name_plural = verbose_name



class mti(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
item = models.ForeignKey(item)
mtd= models.ManyToManyField(mtd)
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 mtiimage(models.Model):
id = models.AutoField(primary_key=True)
mtd = models.ManyToManyField(mtd)
image1 = models.ImageField(verbose_name="Product 1 Image",upload_to='product')
image2 = models.ImageField(verbose_name="Product 2 Image",upload_to='product')
image3 = models.ImageField(verbose_name="Product 3 Image",upload_to='product')
stamp = models.DateTimeField(auto_now=True)

def admin_thumbnail(self):
    return u'<img src="%s" heigh="150" width="200" />' % (self.image1.url)
admin_thumbnail.short_description = 'Thumbnail'
admin_thumbnail.allow_tags = True
def __unicode__(self):
    return u'%s ' % (self.id)
class Meta:
    verbose_name = "MTI Image"
    verbose_name_plural = verbose_name

i have a model called mti(Material Information) which have a list of MTD(Material Description) and in each material description description there is a size and color

i have an MTI id i want to get all the distinct list of color values
mymti = mti.objects.get(pk=1)

how can i get the list of colors from MTI
mymti.mtd.color.name ?

class color(models.Model):
id = models.AutoField(primary_key=True)
name= models.CharField(max_length=255)
stamp= models.DateTimeField(auto_now=True)
def __unicode__(self):
    return self.name
class mtd(models.Model):
id = models.AutoField(primary_key=True)
barcode = models.CharField(max_length=255)
#mti = models.ManyToManyField(mti)
size = models.ForeignKey(size)
color = models.ForeignKey(color)
weight = models.FloatField()
def __unicode__(self):
    return u'%s - %s %s' % (self.barcode,self.color.name,self.size.name)
class Meta:
    verbose_name = "MTD"
    verbose_name_plural = verbose_name



class mti(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
item = models.ForeignKey(item)
mtd= models.ManyToManyField(mtd)
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 mtiimage(models.Model):
id = models.AutoField(primary_key=True)
mtd = models.ManyToManyField(mtd)
image1 = models.ImageField(verbose_name="Product 1 Image",upload_to='product')
image2 = models.ImageField(verbose_name="Product 2 Image",upload_to='product')
image3 = models.ImageField(verbose_name="Product 3 Image",upload_to='product')
stamp = models.DateTimeField(auto_now=True)

def admin_thumbnail(self):
    return u'<img src="%s" heigh="150" width="200" />' % (self.image1.url)
admin_thumbnail.short_description = 'Thumbnail'
admin_thumbnail.allow_tags = True
def __unicode__(self):
    return u'%s ' % (self.id)
class Meta:
    verbose_name = "MTI Image"
    verbose_name_plural = verbose_name

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪心 2024-11-23 01:07:02

注释

使用注释。

mymti = mti.objects.get(pk=1)
unique_colors = mymti.mtd.values_list('color__name', flat=True).annotate()

Annotation

Use annotation.

mymti = mti.objects.get(pk=1)
unique_colors = mymti.mtd.values_list('color__name', flat=True).annotate()
我纯我任性 2024-11-23 01:07:02
colors = mymti.mtd.values_list('color__name', flat=True).distinct()

将导致:

colors = [color1, color2, ...]
colors = mymti.mtd.values_list('color__name', flat=True).distinct()

will result in :

colors = [color1, color2, ...]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文