我还有一个问题。如何解决?我是 Django 的初学者。
我的错误:
/magazines/“杂志”对象的 AttributeError 没有属性“items_set”
class Item(models.Model):
name = models.CharField(max_length=50)
item_price = models.DecimalField(max_digits=5, decimal_places=2)
class Magazine(models.Model):
items = models.ManyToManyField('Item', blank=True, null=True)
owner = models.ForeignKey(User, related_name='u_item')
def show(request): #magazines items and total price of all items in this magazine
user = request.user
magazines = Magazine.objects.filter(owner=user)
for m in Magazine.objects.filter(owner=user):
total = m.items_set.all().annotate(total=Sum('item_price'))
print 'Total cost for items in {0} is {1}'.format(m,total)
return render_to_response('magazines.html', {'magazines': magazines, 'total': total})