我有这样的事情:
class Basket(models.Model):
owner = models.ForeignKey(User, related_name='user_basket', verbose_name='Owner')
name = models.CharField("Basket_Name", max_length=30)
products = models.ManyToManyField('Product', through='BasketProduct', blank=True, null=True)
class BasketProduct(models.Model):
product = models.ForeignKey('Product')
basket = models.ForeignKey('Basket')
quantity = models.IntegerField()
我尝试从用户那里获取所有产品,Basket
并以“笔记本 - 2,PC - 5,电话 - 1”的形式使用他们的价格和名称字符串创建
我的尝试:
user = request.user
b = Basket.objects.filter(owner=user)
bp = BasketProduct.objects.filter(basket=b)
for a in bp:
a = '%s - %s' % (a.product, a.quantity)
print a
这会在 shell 中返回如下内容:
[06/Oct/2012 19:44:41] "GET /list/ HTTP/1.1" 200 0
PC - 3 #single object
Notebook - 1 #second single object
我需要:字符串 =“PC - 3,笔记本 - 1”
请帮忙