2012-03-04 23 views
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)), 
) 

回答

0

我覺得Tastypie的Using Tastypie With Non-ORM Data Sources有你需要的所有信息。

基本上你應該實現負責返回對象的方法(和/或創建,刪除等等,如果你需要的話)。除非需要定製它的功能,否則不需要觸摸「dispatch()」。

這取決於您的需求,但是如果您希望資源像obj_get和obj_get_list一樣行動,則需要實施這兩者。我提到的例子非常好,應該讓你去...