6

我的第一个问题:

所以我正在使用tastepie 为我的应用程序提供api。

我希望能够使用tastepie 呈现json,然后将其包含在django 视图中,以便我可以引导我的应用程序的数据。

在 django sweetpie 食谱中有一个例子: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() 函数来为我提供 json 中的所有(或过滤的)ChatMessageResources。

而且我想使用美味的内部结构,我知道我可以自己序列化它,但这不是重点..

现在的错误是:

NoReverseMatch at /live/

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

我快疯了,我已经尝试了几个小时。

所以,请,如何在 django 视图中通过代码使用 nicepiepie 将 ONE/ALL 资源作为 JSON 获取!

如果不清楚或者我需要澄清,请询问,谢谢

我真正想要做的是能够获取我创建的 API url 返回的 JSON,但是从代码中,而不是通过访问 url .. 所以如果我有/api/v1/messages/?chat_session=14 返回消息列表,我希望能够通过代码执行相同的操作(而不是通过 curl 或其他方式获取 url)。

注意:ModelResource.obj_get 的定义来自https://github.com/toastdriven/django-tastypie/blob/master/tastypie/resources.py

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).")
4

3 回答 3

10

所以在这里我找到了解决方案,问题在于url解析......我需要添加

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})
于 2011-10-18T08:42:38.647 回答
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)
    ....
于 2014-04-16T14:45:23.973 回答
0

你的问题似乎在这里:

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

to 的参数obj_get应该是可以直接传递给标准的 kwargs getNone不应该在那里。

于 2011-10-17T15:07:08.540 回答