Django serializer for one object

后端 未结 3 1386
一个人的身影
一个人的身影 2020-12-29 05:09

I\'m trying to figure out a way to serialize some Django model object to JSON format, something like:

j = Job.objects.get(pk=1)
#############################         


        
3条回答
  •  悲&欢浪女
    2020-12-29 05:41

    How about just massaging what you get back from serializers.serialize? It is not that hard to trim off the square brackets from the front and back of the result.

    job = Job.objects.get(pk=1)
    array_result = serializers.serialize('json', [job], ensure_ascii=False)
    just_object_result = array_result[1:-1]
    

    Not a fancy answer but it will give you just the object in json notation.

提交回复
热议问题