每一個,我有我怎樣才能使Django的返回JSON數據讀取人類能夠
models.py
.......
class ProductsTbl(models.Model):
model_number = models.CharField(
max_length=255,
blank=True,
unique=True,
error_messages={
'unique': "這 model number 已經被註冊了 ."
}
)
name = models.CharField(max_length=255, blank=True, null=True)
material = models.CharField(max_length=255, blank=True, null=True)
color = models.CharField(max_length=255, blank=True, null=True)
feature = models.TextField(blank=True, null=True)
created = models.DateTimeField(editable=False)
modified = models.DateTimeField(auto_now=True)
release = models.DateTimeField(blank=True, null=True)
twtime = models.DateTimeField(blank=True, null=True)
hktime = models.DateTimeField(blank=True, null=True)
shtime = models.DateTimeField(blank=True, null=True)
jptime = models.DateTimeField(blank=True, null=True)
suggest = models.TextField(blank=True, null=True)
description = models.TextField(blank=True, null=True)
cataloggroup = models.ManyToManyField(CatalogGroup)
place = models.ManyToManyField(Place)
scale = models.ManyToManyField(Scale)
slug = models.SlugField(unique=True)
user = models.ForeignKey(User, blank=True, null=True)
useredit = models.CharField(max_length=32, blank=True, null=True)
image = models.ImageField(upload_to=get_imagep_Product, blank=True)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.created = timezone.now()
return super(ProductsTbl, self).save(*args, **kwargs)
和,我工作的API返回的JSON數據這裏是
views.py
from django.http import JsonResponse
from django.core import serializers
.....
def get_index(request):
obj = ProductsTbl.objects.order_by('-created')
data = serializers.serialize("json", obj)
return JsonResponse(data,safe=False)
但是,我得到了回報這樣
我的問題JSON數據是如何才能讓Django的返回JSON數據的人讀能這樣
感謝誰回答我的任何一個
您序列化兩次;一次在串行器調用中,一次在JsonResponse中。不要這樣做。 –
你的意思是我「從django.core進口序列化」和「數據= serializers.serialize(」 JSON「 OBJ)」? –
'serializers.serialize( 'JSON',OBJ)'轉換對象到JSON字符串。然後'JsonResponse'再次將它轉換爲一個字符串*;所以內容是雙重逃脫的。 –