Skip to main content
 首页 » 编程设计

python之Tasty-Pie之在不使用 full=True 的情况下拉入相关领域

2025年05月04日13fff_TT

我有一个应用程序使用 Python 请求来查询启用了 Tasty-Pie 的 Django 应用程序。

我有一个名为 Application 的模型,具有相应的 Tasty-Pie 资源。

这个模型/资源有几个外键,将应用程序链接到其他模型(例如二进制、主机、托管等)

我正在使用 Tasty-Pie 过滤器获取应用程序的一个子集,然后我想打印一个漂亮的应用程序表,以及来自那些相关模型的一些字段。

现在,我正在使用以下方法获取应用程序表:

def get_applications(self, parsed_args): 
    r = requests.get('http://foobar.com:8000/api/v1/application/?name__iregex={0}&format=json'.format(parsed_args.applications)) 
    print(r.url) 
    return r   
def application_iter(self, parsed_args): 
    for application in self.get_applications(parsed_args).json['objects']: 
        yield (application['name'], application['author'], application['some_other_field']) 
def take_action(self, parsed_args): 
    return(('Name', 'Author', 'Some Other Field), 
            self.application_iter_iter(parsed_args), 
        ) 

我的问题是,引入所有相关领域的“推荐”或惯用方式是什么?有没有办法扩展上面的方法来做到这一点?

我的印象是 full=True 是一种不好的做法,而使用资源 URI 是更好的方法。

我怎样才能做到这一点,同时最大限度地减少请求和数据库命中的数量?

干杯, 维克多

请您参考如下方法:

为什么你认为 full=True 不好?

https://django-tastypie.readthedocs.org/en/latest/resources.html#why-resource-uris

Ideology aside, you should use whatever suits you. If you prefer fewer requests & fewer endpoints, use of full=True is available, but be aware of the consequences of each approach.

如果它可以清晰地阅读并且它符合您的要求,您可以随心所欲。 "full=True"是供开发人员使用的