I have a django model called ServiceSubCategory and I want to create a JSON list of its primary key values using Python.
I have tried this:
idDic=[obj.as_json for obj in ServiceSubCategory.objects.values_list('id',flat=True)]
But I'm getting this error:
intobject has no attributeas_json
I'm doing this because I'm gonna append another JSON to this later.
So how could I create a JSON out of my models primery Keys?
Update
I tried
id_json = json.dumps(ServiceSubCategory.objects.values_list('id',flat=True))
And I'm getting a new error:
[1,2,3,4,5,6,7,8,9,10] is not JSON serializable
And this is the traceback:
Traceback: File "/usr/local/lib/python2.7/dist-packages/Django-1.7-py2.7.egg/django/core/handlers/base.py" in get_response 111. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/var/www/html/salimi/salimi/views.py" in service 61. idDic=json.dumps(ServiceSubCategory.objects.values_list('id',flat=True)) File "/usr/lib/python2.7/json/init.py" in dumps 243. return _default_encoder.encode(obj) File "/usr/lib/python2.7/json/encoder.py" in encode 207. chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python2.7/json/encoder.py" in iterencode 270. return _iterencode(o, 0) File "/usr/lib/python2.7/json/encoder.py" in default 184. raise TypeError(repr(o) + " is not JSON serializable")
Exception Type: TypeError at /service/1/ Exception Value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is not JSON serializable