2016-07-22 53 views
-1

每一個,我有我怎樣才能使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) 

但是,我得到了回報這樣

enter image description here

我的問題JSON數據是如何才能讓Django的返回JSON數據的人讀能這樣

enter image description here

感謝誰回答我的任何一個

+0

您序列化兩次;一次在串行器調用中,一次在JsonResponse中。不要這樣做。 –

+0

你的意思是我「從django.core進口序列化」和「數據= serializers.serialize(」 JSON「 OBJ)」? –

+1

'serializers.serialize( 'JSON',OBJ)'轉換對象到JSON字符串。然後'JsonResponse'再次將它轉換爲一個字符串*;所以內容是雙重逃脫的。 –

回答

0

用戶DRF api_v IEW

from rest_framework.decorators import api_view 
from rest_framework.response import Response 

@api_view() 
def get_index(request): 

    obj = ProductsTbl.objects.order_by('-created') 
    data = serializers.serialize("json", obj) 

    return Response(data) 
+0

感謝您的回覆,我已經PIP安裝DRF,但我得到「無法在應用DjangoModelPermissions認爲不設置'.queryset'或具有'.get_queryset()'方法「。 –

0

它可能爲你工作,改變你的views.py如下:

from django.http import JsonResponse 
from django.core import serializers 
..... 
def get_index(request): 

    obj = ProductsTbl.objects.order_by('-created') 
    data = self.stdout.write(serializers.serialize("json", 
             obj, 
             indent=2)) 

    return HttpResponse(json.dumps(data),content_type="application/json") 

還是看this你會得到一些想法。