简体   繁体   中英

Django Query Set into json object

Below I'm trying to get a query make is a json object, so that in my template using jQuery I can loop over it.

My View

from django.core import serializers
objectQuerySet = Recipient.objects.filter(incentiveid=incentive).values("mobile", "countryid")
data = serializers.serialize("json", objectQuerySet)

return render_to_response('smssend.html', context_instance=RequestContext(request))

I'm getting the following error.

Non-model object (<type 'dict'>) encountered during serialization

Request Method: 

why?

#values() "returns dictionaries when used as an iterable" - https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values

I think you need python's json#dumps(dict) here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM