[英]Django Multiple ForeignKey Navigation
我正在建立一个购物车。 在我的购物车中,一个项目可以由其他项目组成。 我需要在单个模板中显示一组项目及其相应的关联部分。 我知道如何在模板中显示单个项目及其对应的零件,但是我似乎无法弄清楚如何显示多个零件,每个零件都有自己的零件清单。
我在模板文件中摆弄了标签的每个排列:
# checkout.html
{% for item in cart_items %}
<tr>
<td class="left">
{{ item.name }}
<ul>
{% for part in item.product.buildpart.part_set.all %}
<li>{{ part.name }}
{% endfor %}
</ul>
</td>
<td>${{ item.price }}</td>
<td>{{ item.quantity }}</td>
<td class="right">${{ item.lineItemTotal }}</td>
</tr>
{% endfor %}
这是生成模板的信息:
# views.py
def checkout(request):
cart_items = get_cart_items(request)
<snip>
return render(request, 'checkout.html', locals())
这是get_cart_items()函数,该函数返回用户购物车中的所有商品:
# cart.py
def get_cart_items(request):
""" return all items from the current user's cart """
return CartItem.objects.filter(cart_id=get_cart_id(request))
这是CartItem模型:
# models.py
class Item(models.Model):
cart_id = models.CharField(max_length=50)
quantity = models.IntegerField(default=1)
product = models.ForeignKey(PartModel, unique=False)
class Meta:
abstract = True
<snip>
class CartItem(Item):
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['date_added']
verbose_name = "Cart Item"
<snip>
“产品”字段是PartModel模型的外键:
# models.py
class PartModel(models.Model):
family = models.ForeignKey(PartFamily)
name = models.CharField("Model Name", max_length=50, unique=True)
slug = models.SlugField(help_text="http://www.Knowele.com/<b>*slug*</b>",
unique=True)
<snip>
buildpart = models.ManyToManyField('self', through='BuildPart',
symmetrical=False, related_name='+')
class Meta:
ordering = ['name']
verbose_name = "Product Model"
<snip>
PartModel模型通过buildpart字段和BuildPart模型与自身具有ManyToMany关系,以促进可以由其他目录项组成的目录项的概念:
# models.py
class Build(models.Model):
build = models.ForeignKey(PartModel, related_name='+')
part = models.ForeignKey(PartModel, related_name='+')
quantity = models.PositiveSmallIntegerField(default=1)
class Meta:
abstract = True
unique_together = ('build', 'part')
def __unicode__(self):
return self.build.name + ' with ' + str(self.quantity) + ' * ' + \
self.part.family.make.name + ' ' + self.part.name
class BuildPart(Build):
pass
class Meta:
verbose_name = "Build Part"
我似乎无法在模板(上面列出)中进行必要的ForeignKey遍历,以获取与CartItem模型中与用户项目相关联的所有零件。 是我在模板中没有正确执行操作还是在视图中没有包装正确的QuerySet?
此问题的第二部分是,一旦我获得了这些零件,就需要它们按照PartType模型的“ order”整数字段中指定的顺序显示:
# models.py
class PartType(models.Model):
name = models.CharField("Part Type", max_length=30, unique=True)
slug = models.SlugField(unique=True)
order = models.PositiveSmallIntegerField()
description = models.TextField(blank=True, null=True)
class Meta:
ordering = ['name']
verbose_name = "Product Type"
def __unicode__(self):
return self.name
class PartFamily(models.Model):
make = models.ForeignKey(PartMake)
type = models.ForeignKey(PartType)
name = models.CharField("Family Name", max_length=30,
unique=True)
slug = models.SlugField(unique=True)
url = models.URLField("URL", blank=True, null=True)
description = models.TextField(blank=True, null=True)
class Meta:
ordering = ['name']
verbose_name = "Product Family"
verbose_name_plural = "Product Families"
def __unicode__(self):
return self.name
正如您所看到的,在PartModel模型中,“族”字段是PartFamily模型的外键,在PartFamily模型中,“类型”字段是PartType模型的外键,其中最重要的是“订单”字段中需要订购的零件。
我希望这是有道理的,您会明白为什么对于像我这样的菜鸟来说,如此复杂。
只需迭代item.product.buildpart.all
:
{% for item in cart_items %}
[...]
{% for part in item.product.buildpart.all %}
{{ part.name }}[...]
{% endfor %}
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.