python - send report in specific json format -
views.py
def json(request): defaultnumber = [] phoneinfo = phoneinfo.objects.filter(user = user_id) phone in phoneinfo: phone_no = {'id':some.id, 'name1':phone.name1, 'number1':phone.number1, 'name2':phone.name2, 'number2':phone.number2, } } defaultnumber.append(phone_no) result = { 'phone':defaultnumber} return httpresponse(json.dumps(result), mimetype="application/json")
i need send data json format.
use model_to_dict
instead:
from django.forms.models import model_to_dict def json_view(request): phoneinfo = phoneinfo.objects.filter(user = user_id) phones = [model_to_dict(phone) phone in phoneinfo] result = {'phoneinfo': phones} return httpresponse(json.dumps(result), mimetype="application/json")
and, don't call view json
- overriding json
module name.
and, it's unclear user_id
variable comes.
Comments
Post a Comment