0

i am using django 1.7.1,i got an error "is not JSON serializable".Here is my code

def Project_info(request):
        project_id=request.GET.get('project_id')
        people = Project_Management.objects.all().values()
        print people
        return returnSuccessShorcut ({'people': people })



def returnresponsejson(pass_dict, httpstatus=200):
    json_out = simplejson.dumps(pass_dict)
    return HttpResponse(json_out, status=httpstatus, content_type="application/json")


def returnSuccessShorcut(param_dict={}, httpstatus=200):
    param_dict['success'] = True
    return returnresponsejson(param_dict, httpstatus)

and console output is:-

[{'abbreviation': u'IOS', 'acid': None, 'end_date': datetime.datetime(2014, 7, 1, 4, 59, 59, tzinfo=<UTC>), 'start_date': datetime.datetime(2014, 3, 21, 5, 0, tzinfo=<UTC>), 'user_story_id': None, 'project_name': u'2014 -KHL-347/Khaylo', 'modify_date': None, 'project_id': u'67375', 'user_name': None, 'id': 1L, 'isActive': None}]

But when I hit the api in browser I got the above error. Kindly suggest the solutions.

fedorqui 'SO stop harming'
  • 228,878
  • 81
  • 465
  • 523
jademaddy
  • 89
  • 6
  • which line in code gives the error? – akonsu Jan 05 '15 at 15:28
  • in this line:-def returnSuccessShorcut(param_dict={},httpstatus=200): – jademaddy Jan 05 '15 at 15:29
  • http://stackoverflow.com/questions/16336271/is-not-json-serializable – akonsu Jan 05 '15 at 15:31
  • Not related to your question, but in the line `def returnSuccessShorcut(param_dict={}, httpstatus=200):` you are using a dictionary as a default value for a parameter. Don't do that as any time you make use of the default the same dictionary will be shared between all calls to the function. – Duncan Jan 05 '15 at 15:38

1 Answers1

0

You need to convert people to a list before serialize it, because ValuesQuerySet returned by QuerySet.values cannot be serialized by default.

def Project_info(request):
    project_id=request.GET.get('project_id')
    people = Project_Management.objects.all().values()
    people = list(people)
    return returnSuccessShorcut ({'people': people })
falsetru
  • 314,667
  • 49
  • 610
  • 551
  • datetime.datetime(2014, 7, 1, 4, 59, 59, tzinfo=) is not JSON serializable – jademaddy Jan 05 '15 at 15:32
  • 1
    @user3762689, In short: use `from django.core.serializers.json import DjangoJSONEncoder; json_out = simplejson.dumps(pass_dict, cls=DjangoJSONEncoder)` – falsetru Jan 05 '15 at 15:36