0
我學習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 %}
如何顯示玩具的價格模板?
謝謝。很有幫助 – k44