Django Invalid JSON Response -


the following code produces json given below. when validate json lint, invalid. doing wrong here?

  def json_candidate_get(request, model, m_id=none):      response = {'message' : 'incorrect json'}      try:          obj = model.objects.filter(pk=m_id)         ce = candidate_profiles.objects.filter(pk=m_id)         cw = candidate_company_profiles.objects.filter(pk=m_id)         response = json.dumps({  'technologiesvalue':[],'technologies': []  })       except exception e:         logging.exception("exception"+str(e))      return response      @is_login()   def candidate_create(request,m_id=none,token=none):      response_data = {'message': 'unsuccessfull'}      if token not none:         try:            if request.method == 'get':               response_data = json_candidate_get(request,candidates,m_id)               print response_data               #response_data = serializers.serialize('json', response_data)         except exception e:            logging.exception(e)       return httpresponse(response_data, content_type="application/json") 

json:

{ technologiesvalue: [0] technologies: [0]  } 

the json.dumps function meant convert python objects a json string. serializing model instances (via querysets) json strings, , json.dumps trying convert these strings json on again--it knows you've passed str objects, not these str objects represents json.

the json encoder used json.dumps knows how convert handful of built-in types:

so need convert model instances 1 of these types. easiest solution use django.forms.models.model_to_dict on each element of querysets, so:

from django.forms.models import model_to_dict response = json.dumps({    'candidate': [model_to_dict(x) x in obj],    'candidateeducationprofile': [model_to_dict(x) x in ce],    ... ) 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -