2011-10-17 78 views
6

我的第一個問題在這裏:在視圖中使用tastypie資源

所以我使用tastypie來爲我的應用程序提供api。

我希望能夠使用tastypie呈現json,然後將其包含在django視圖中,以便引導我的應用程序的數據。

還有就是這個在Django tastypie食譜這裏一個例子:http://django-tastypie.readthedocs.org/en/latest/cookbook.html#using-your-resource-in-regular-views

的問題是,我不能得到這個工作,我已經從簡單到複雜的嘗試變種,我只是不能得到它,在這裏我的模型的一些代碼:

class ChatMessage(models.Model): 
    content = models.TextField() 
    added = models.DateTimeField(auto_now_add=True) 

    author = models.ForeignKey(ChatUser, related_name="messages") 
    chat_session = models.ForeignKey(ChatSession, related_name="messages") 
    answer_to = models.ForeignKey('self', blank=True, null=True) 

    flagged = models.BooleanField(blank=True,default=False) 
    mododeleted = models.BooleanField(blank=True,default=False) 
    mododeleted_by = models.ForeignKey(ChatUser,blank=True,null=True,default=None) 
    mododeleted_at = models.DateTimeField(blank=True,null=True,default=None) 
    [...] 

class ChatSession (models.Model): 
    title = models.CharField(max_length=200) 
    link_title = models.CharField(max_length=200) 
    description = tinymce_models.HTMLField() 
    date = models.DateTimeField() 
    online = models.BooleanField(default=False) 
    next_session = models.BooleanField(default=False) 
    meps = models.ManyToManyField(ChatMep) 
    uid_newsupdate = models.CharField(max_length=200,blank=True,null=True,default="") 
    [...] 

和我的資源:

class ChatMessageResource(MyModelResource): 
    chat_session = fields.ForeignKey(ChatSessionResource, 'chat_session') 

    def renderOne(self,request,pkval): 
     data = self.obj_get(None,pk=pkval) 
     dbundle = self.build_bundle(obj=data,request=request) 
     return self.serialize(None,self.full_dehydrate(dbundle),'application/json') 

    def dehydrate(self, bundle): 
     bundle.data['likes'] = bundle.obj.get_likes() 
     bundle.data['likes_count'] = len(bundle.data['likes']) 
     return bundle 

    class Meta: 
     authentication = Authentication() 
     authorization = Authorization() 
     queryset = ChatMessage.objects.all() 
     resource_name = 'message' 
     fields = ('content', 'added', 'flagged', 'mododeleted','author','answer_to','chat_session') 
     filtering = { 
      'chat_session': ALL_WITH_RELATIONS, 
     } 

和我的視圖索引:

def index(request): 

    cur_sess = get_current_chat_session() 

    data1= ChatMessageResource().renderOne(request,723) 

    return render_to_response('test.html', 
          { 
          'all_data' : data1 
          }, 
          context_instance=RequestContext(request)) 

我想什麼是我的renderOne()函數來給我一個ChatMessageResource 的JSON和我也想一個renderAll()函數來GICE我所有(或過濾)的JSON ChatMessageResources。

而且我想用tastypie內部,我知道我可以通過自己的序列化,但是這不是問題的關鍵..

眼下的錯誤是:

NoReverseMatch at /live/ 

Reverse for 'api_dispatch_detail' with arguments '()' and keyword arguments '{'pk': 14L, 'resource_name': 'session'}' not found. 

我只是越來越瘋狂,我一直在努力幾個小時。

所以,請大家,如何通過在django視圖中使用tastypie的代碼獲取ONE/ALL資源作爲JSON!

如果目前尚不清楚,或者我需要澄清,請你只問,感謝

真的是我想要做的是能得到JSON由我創建了一個API URL返回,但是從代碼,而不是通過訪問URL ..所以,如果我有/api/v1/messages/?chat_session=14哪些返回的消息列表,我希望能夠做到這一點的代碼(而不是通過curl或別的東西取得URL)。

注:從https://github.com/toastdriven/django-tastypie/blob/master/tastypie/resources.py ModelResource.obj_get的 定義

def obj_get(self, request=None, **kwargs): 
      """ 
    A ORM-specific implementation of ``obj_get``. 

    Takes optional ``kwargs``, which are used to narrow the query to find 
    the instance. 
    """ 
      try: 
       base_object_list = self.get_object_list(request).filter(**kwargs) 
       object_list = self.apply_authorization_limits(request, base_object_list) 
       stringified_kwargs = ', '.join(["%s=%s" % (k, v) for k, v in kwargs.items()]) 

       if len(object_list) <= 0: 
        raise self._meta.object_class.DoesNotExist("Couldn't find an instance of '%s' which matched '%s'." % (self._meta.object_class.__name__, stringified_kwargs)) 
       elif len(object_list) > 1: 
        raise MultipleObjectsReturned("More than '%s' matched '%s'." % (self._meta.object_class.__name__, stringified_kwargs)) 

       return object_list[0] 
      except ValueError: 
       raise NotFound("Invalid resource lookup data provided (mismatched type).") 

回答

9

所以在這裏我找到了解決辦法,問題與解決網址...我需要

def get_resource_uri(self, bundle_or_obj): 
    return '/api/v1/%s/%s/' % (self._meta.resource_name,bundle_or_obj.obj.id) 

,以增加對相關對象(會話這裏),它的工作(不問爲什麼!)

所以這裏是renderDetail和renderList我工作的解決方案:

def renderDetail(self,pkval): 
    request = HttpRequest() 
    request.GET = {'format': 'json'} 
    resp = self.get_detail(request, pk=pkval) 
    return resp.content 


def renderList(self,options={}): 
    request = HttpRequest() 
    request.GET = {'format': 'json'} 
    if len(options) > 0: 
     request.GET.update(options) 

    resp = self.get_list(request) 
    return resp.content 

而且這裏有一個例子用法:

cmr = ChatMessageResource() 

dataOne= cmr.renderDetail("723") 

dataAll = cmr.renderList({'limit':'0','chat_session':cur_sess.pk}) 
+0

我知道我可以繞過請求的創建和響應創作,但我認爲這是比較容易 – dwarfy

+4

一個更簡單的方法是在反向URL時指定API名稱,例如'url = reverse('api_dispatch_list',kwargs = {'resource_name':'myresource','api_name':'v1'})'。這意味着如果您更改API根URL或版本號,代碼將繼續工作。 –

+0

好的謝謝,我會試試... – dwarfy

0

你的問題似乎是在這裏:

data = self.obj_get(None,pk=pkval) 

的參數obj_get應該是可以直接傳遞到標準kwargs getNone不應該在那裏。

+0

我嘗試沒有它,但它不會改變..反正我把ModelResource.obj_get的來源上面 – dwarfy

2

https://github.com/toastdriven/django-tastypie/issues/962

我發現obj_get方法需要捆綁請求對象。查看鏈接。

def user_detail(request, username): 
    ur = UserResource() 
    # Add this request bundle to the obj_get() method as shown. 
    req_bundle = ur.build_bundle(request=request) 
    user = ur.obj_get(req_bundle, username=username) 
    ....