I have the following code for serializing the queryset:
def get_shop_categories(request):
if request.method == 'POST':
parent_id = int(request.POST.get('parent_id'))
categories = (ShopCategory.objects.filter(enabled=True, parent=parent_id).values('id', 'title'))
json_posts = json.dumps(categories)
return HttpResponse(
json_posts,
content_type="application/json"
)
else:
return HttpResponse(
json.dumps({"success": False}),
content_type="application/json"
)
What I want it returns is this:
[{'id': 2, 'title': 'Tennis'}, {'id': 4, 'title': 'Basket'}]
Instead I'm getting this error:
TypeError at /ajax/get_shop_categories
[{'id': 2, 'title': 'Tennis'}, {'id': 4, 'title': 'Basket'}] is not JSON serializable
I also used serialize in this way:
categories = ShopCategory.objects.filter(enabled=True, parent=parent_id)
#json_posts = json.dumps(categories)
#objectQuerySet = ConventionCard.objects.filter(ownerUser = user)
json_posts = serializers.serialize('json', list(categories), fields=('id', 'title'))
but what I get I don't like:
[{"fields":{"title":"Tennis"},"pk":2,"model":"appname.shopcategory"},{"fields":{"title":"Basket"},"pk":4,"model":"appname.shopcategory"}]