0
在下面的示例中URL被覆蓋,以便我可以傳遞類似於 http://localhost:8000/api/v1/entry/1234/1233/?format=json的內容,這樣做會將參數傳遞給WRAP VIEW,當我執行self.create_response(request,data)時,瀏覽器僅顯示類似如何讓VIEW返回序列化數據?
在0x2e27a50 在瀏覽器
api.Entry對象,領域沒有得到連載: 注:我不需要ModelResource在這個階段。我如何使自定義視圖dispatch_data返回像obj_get和get_object_list這樣的數據?以下是有問題的代碼:
#Object class Class Entry(object) name = '' #Resource class class EntryResource(Resource): name = fields.CharField(attribute = 'name') class Meta: resource_name = 'entry' object_class = Entry include_resource_uri = False authentication = Authentication() authorization = Authorization() serializer = Serializer() def override_urls(self): return [url(r"^(?P<resource_name>%s)/(?P<p1>[\d]{4})/(?P<p2>[\d]{4})%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('dispatch_data'),name='api_dispatch_data'),] def dispatch_data(self, request, **kwargs): p1 = kwargs['p1'] #params can be retrieved here p1 = kwargs['p2'] info = Entry() info.name = p1 #just example response = {1:info} return info.values() #Above results in ERROR, it will say Entry object has no attribute 'has_header' #changing to self.create_response(request, info) will not serialize the fields #urls.py api = Api(api_name='v1') api.register(EntryResource()) urlpatterns = patterns('', url(r'^api/', include(api.urls)), )