摘要:問題是我無法從序列化模型中的嵌套類訪問縮略圖圖像,我不知道如何在JSON響應中公開它。使用嵌套類序列化模型到json不會暴露嵌套類
描述:我想序列化我的Thing模型到JSON,它的工作方式,以一種方式。我得到我的JSON響應如下:
// JSON response
[
{
pk: 1
model: "base.thing"
fields: {
active: true
created_at: "2011-04-13 07:18:05"
num_views: 1
file: "things/5216868239_b53b8d5e80_b.jpg"
title: "ding ding ding"
}
}
]
我剛開始使用Django的imagekit操縱事情圖像縮略圖的大小和它的工作原理在正常使用,即在模板中運行「thing.thumbnail_image.url」將正確的網址返回到縮略圖圖像。
沙箱代碼我玩弄:
# base/models.py
from django.db import models
from imagekit.models import ImageModel
class Thing(ImageModel):
title = models.CharField(max_length=30)
file = models.ImageField(upload_to='things')
created_at = models.DateTimeField(auto_now_add=True)
active = models.BooleanField()
num_views = models.PositiveIntegerField(editable=False, default=0)
def __unicode__(self):
return self.title
class IKOptions:
spec_module = 'base.specs'
image_field = 'file'
save_count_as = 'num_views'
# base/views.py
from django.views.generic.base import View
from django.views.generic.list import MultipleObjectTemplateResponseMixin, ListView
from base.models import Thing
from django import http
from django.utils import simplejson as json
from utils import JsonResponse
class JSONResponseMixin(object):
def render_to_response(self, context):
"Returns a JSON response containing 'context' as payload"
return self.get_json_response(context)
def get_json_response(self, content, **httpresponse_kwargs):
"Construct an `HttpResponse` object."
return JsonResponse(content['thing_list']) # I can't serialize the content object by itself
class ThingsView(JSONResponseMixin, ListView):
model = Thing
context_object_name = "thing_list"
template_name = "base/thing_list.html"
def render_to_response(self, context):
if self.request.GET.get('format', 'html') == 'json':
return JSONResponseMixin.render_to_response(self, context)
else:
return ListView.render_to_response(self, context)
# base/specs.py
from imagekit.specs import ImageSpec
from imagekit import processors
class ResizeThumb(processors.Resize):
width = 100
height = 75
crop = True
class ResizeDisplay(processors.Resize):
width = 600
class EnchanceThumb(processors.Adjustment):
contrast = 1.2
sharpness = 1.1
class Thumbnail(ImageSpec):
access_as = 'thumbnail_image'
pre_cache = True
processors = [ResizeThumb, EnchanceThumb]
class Display(ImageSpec):
increment_count = True
processors = [ResizeDisplay]
# utils.py
from django.core.serializers import json, serialize
from django.db.models.query import QuerySet
from django.http import HttpResponse
from django.utils import simplejson
class JsonResponse(HttpResponse):
def __init__(self, object):
if isinstance(object, QuerySet):
content = serialize('json', object)
else:
content = simplejson.dumps(
object, indent=2, cls=json.DjangoJSONEncoder,
ensure_ascii=False)
super(JsonResponse, self).__init__(
content, content_type='application/json')
我明白任何幫助,它已經阻止了我一天。
最佳關於下述
使用的版本:
的Django 1.3
PIL 1.1.7
Django的imagekit 0.3.6
simplejson 2.1.3
我也想不出 - 爲什麼 - 我想做這樣的事情,我主要想到,有可能讓JSON反映完整的對象結構。 – varl 2011-05-31 08:40:11