简体   繁体   English

Tastypie获得全部资源仅在第二次有效

[英]Tastypie get full resource only works the second time

I'm developing an Android application with backend developed using Tastypie and Django. 我正在开发一个Android应用程序,该应用程序的后端是使用Deliciouspie和Django开发的。 I have a get request for which I want to optionally be able to retrieve the entire object (with complete related fields, rather than URIs). 我有一个get请求,希望可以有选择地检索整个对象(具有完整的相关字段,而不是URI)。 Below is part of the python code for the resource I'm talking about: 以下是我正在谈论的资源的python代码的一部分:

class RideResource(ModelResource):

    user = fields.ForeignKey(UserResource, 'driver')
    origin = fields.ForeignKey(NodeResource, 'origin', full=True)
    destination = fields.ForeignKey(NodeResource, 'destination', full=True)
    path = fields.ForeignKey(PathResource, 'path')

    # if the request has full_path=1 then we perform a deep query, returning the entire path object, not just the URI
    def dehydrate(self, bundle):
        if bundle.request.GET.get('full_path') == "1":
            self.path.full = True
        else:
            ride_path = bundle.obj.path
            try:
                bundle.data['path'] = _Helpers.serialise_path(ride_path)
            except ObjectDoesNotExist:
                bundle.data['path'] = []
        return bundle

As you can see the RideResource has a foreign key pointing to PathResource. 如您所见,RideResource具有指向PathResource的外键。 I'm using the dehydrate function to be able to inspect if the GET request has a parameter "full_path" set to 1. In that case I set programmatically the path variable to full=True . 我正在使用脱水功能来检查GET请求是否将参数“ full_path”设置为1。在这种情况下,我以编程方式将path变量设置为full=True Otherwise I simply return the path URI. 否则,我只是返回路径URI。

The thing is that the code seems to work only the second time the GET is performed. 事实是,该代码似乎仅在第二次执行GET时才起作用。 I've tested it hundreds of times and, when I perform my GET with full_path=1 , even tho it enters the if and sets self.path.full = True , the first time it only returns the URI of the PathResource object. 我已经对其进行了数百次测试,当我使用full_path=1执行GET时,即使它进入if并设置self.path.full = True ,也只有在它第一次仅返回PathResource对象的URI时。 While, if I relaunch the exact same request a second time it works perfectly... 同时,如果我第二次重新启动完全相同的请求,则效果很好...

Any idea what's the problem? 知道有什么问题吗?

EDIT AFTER SOLUTION FOUND THANKS TO @Tomasz Jakub Rup 解决方案编辑后感谢@Tomasz Jakub Rup

I finally managed to get it working using the following code: 我终于设法使用以下代码使其工作:

def full_dehydrate(self, bundle, for_list=False):
    self.path.full = bundle.request.GET.get('full_path') == "1"
    return super(RideResource, self).full_dehydrate(bundle, for_list)

def dehydrate(self, bundle):
    if not bundle.request.GET.get('full_path') == "1":
        try:
            bundle.data['path'] = _Helpers.serialise_path(bundle.obj.path)
        except ObjectDoesNotExist:
            bundle.data['path'] = []
    return bundle

dehydrate is called after full_dehydrate . dehydratefull_dehydrate dehydrate Overwrite full_dehydrate function. 覆盖full_dehydrate函数。

def full_dehydrate(self, bundle, for_list=False):
    self.path.full = bundle.request.GET.get('full_path') == "1"
    return super(RideResource, self).full_dehydrate(bundle, for_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM