我学习了 Django ORM。
class Toy(models.Model):
name = models.CharField(max_length=20)
desc = models.TextField()
price = models.DecimalField(max_digits=5, decimal_places=2)
class Box(models.Model):
name = models.CharField(max_length=20)
proprietor = models.ForeignKey(User, related_name='User_Box')
toys = models.ManyToManyField(Toy, blank=True)
我在视图中尝试这样的事情:
def show_box(request):
user = request.user
box = Box.objects.filter(proprietor = user)
return render_to_response('box.html', {'box': box})
在模板中:
{% for b in box %}
Toy: {% for toys in b.toys.all %}{{ toys }}<br/>{% endfor %}
Price:
{% endfor %}
如何在模板中显示玩具的价格?