Reputation: 765
I have the following models:
class Product(models.Model):
objects = ProductManger()
name = models.CharField(max_length=200)
brand_name = models.CharField(max_length=100)
description = models.TextField()
image_url = models.CharField(max_length=200)
class Cart(models.Model):
user = models.ForeignKey(User)
creation_date = models.DateTimeField(auto_now_add=True)
modification_date = models.DateTimeField(auto_now=True, auto_now_add=True)
is_check_out = models.BooleanField(default=False)
class CartItem(models.Model):
objects = CartItemManager()
cart = models.ForeignKey(Cart)
product = models.ForeignKey(Product)
quantity = models.PositiveSmallIntegerField(default=1)
I want to get a JSON of all the cartItems in a cart (where is_check_out =False), and for that i have the following code:
def get_user_cart(self, request, **kwargs):
self.method_check(request, allowed=['get'])
self.is_authenticated(request)
cart, created = Cart.objects.get_or_create(user__exact=request.user, is_check_out=False)
if not created:
items = CartItem.objects.filter(cart=cart)
d = []
for item in items:
print item.product
d.append(model_to_dict(item))
data = [ { 'cart_id':cart.id, 'items':d} ]
else:
data = [ { 'cart_id':cart.id, 'items':[]} ]
data_string = json.dumps(data, cls=DjangoJSONEncoder)
return HttpResponse(data_string, mimetype='application/json')
This gives me the following JSON:
[
{
"items": [
{
"product": 48,
"price": "0.69",
"tax": "0.09",
"cart": 169,
"note": null,
"id": 467,
"quantity": 1
}
],
"cart_id": 169
}
]
But what i am looking for is something like :
[
{
"items": [
{
"product": {
"id": 1,
"name": "apple",
"image_url": "http://localhost"
},
"price": "0.69",
"tax": "0.09",
"cart": 169,
"note": null,
"id": 467,
"quantity": 1
}
],
"cart_id": 169
}
]
UPDATE:
I changed my get_user_cart
to the following to achieve what i was looking for; if there is a better way of doing this please comment or answer.
def get_user_cart(self, request, **kwargs):
self.method_check(request, allowed=['get'])
self.is_authenticated(request)
cart, created = Cart.objects.get_or_create(user__exact=request.user, is_check_out=False)
if not created:
items = CartItem.objects.select_related('product').filter(cart=cart)
d = []
for item in items:
d.append({
"product": {
"id": item.product.id,
"name": item.product.name,
"image_url": item.product.image_url
},
'price': item.price,
'tax': item.tax,
'cart':item.cart.id,
'note': item.note,
'id':item.id,
'quantity':item.quantity
})
data = [ { 'cart_id':cart.id, 'items':d} ]
else:
data = [ { 'cart_id':cart.id, 'items':[]} ]
data_string = json.dumps(data, cls=DjangoJSONEncoder)
return HttpResponse(data_string, mimetype='application/json')
Any help is appreciated
Upvotes: 1
Views: 4784
Reputation: 3413
Instead of using filter
, it would appear that you need to use select_related
so that the query gets that data as well.
Based on your code, you only need to change one line in the loop:
items = CartItem.objects.select_related('product').filter(cart=cart)
Upvotes: 1