django orm 深度搜索

发布于 2024-11-16 08:07:57 字数 2294 浏览 0 评论 0原文

三个课程

class A(models.Model):
id = models.AutoField(primary_key=True)
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')
color = models.ForeignKey(color)
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

class B(models.Model):
id = models.AutoField(primary_key=True)
barcode = models.CharField(max_length=255)
a = models.ForeignKey(A)
size = models.ForeignKey(size)
weight = models.FloatField()
def __unicode__(self):
    return u'%s - %s %s' % (self.barcode,self.mtiimage.image1,self.size.name)
class Meta:
    verbose_name = "MTD"
    verbose_name_plural = verbose_name

class C(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
item = models.ForeignKey(item)
b= models.ManyToManyField(B)
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

有我想要的

myC = c.objects.get(pk=1)

伙计们,我在模板中 将使用三个循环来显示数据

for tmpc in myC:
    for tmpb in tmpc.B:
        for tmpa in tmpb.A
            print tmpa.image1

我尝试了很多次但失败了

myC = C.objects.get(pk=1)
myC.B
<django.db.models.fields.related.ManyRelatedManager object at ....

guys i have three classes

class A(models.Model):
id = models.AutoField(primary_key=True)
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')
color = models.ForeignKey(color)
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

class B(models.Model):
id = models.AutoField(primary_key=True)
barcode = models.CharField(max_length=255)
a = models.ForeignKey(A)
size = models.ForeignKey(size)
weight = models.FloatField()
def __unicode__(self):
    return u'%s - %s %s' % (self.barcode,self.mtiimage.image1,self.size.name)
class Meta:
    verbose_name = "MTD"
    verbose_name_plural = verbose_name

class C(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
item = models.ForeignKey(item)
b= models.ManyToManyField(B)
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

what i want

myC = c.objects.get(pk=1)

in template
will use three loops to display data

for tmpc in myC:
    for tmpb in tmpc.B:
        for tmpa in tmpb.A
            print tmpa.image1

i tried many time but i failed

myC = C.objects.get(pk=1)
myC.B
<django.db.models.fields.related.ManyRelatedManager object at ....

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

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

发布评论

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

评论(1

以为你会在 2024-11-23 08:07:57
for tmpc in myC:
    for tmpb in tmpc.B.all():
        for tmpa in tmpb.A.all():
            print tmpa.image1

并在你使用的时候给你的类起一个合适的名字;)

for tmpc in myC:
    for tmpb in tmpc.B.all():
        for tmpa in tmpb.A.all():
            print tmpa.image1

And give your classes a decent name while you're at it ;)

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