2

我有一个使用 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 是一种更好的方法。

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

干杯,维克多

4

1 回答 1

0

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

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

除了意识形态,你应该使用适合你的任何东西。如果您喜欢更少的请求和更少的端点,可以使用 full=True,但请注意每种方法的后果。

如果它可以清晰地阅读并且可以执行您想要的操作,您可以做任何您喜欢的事情。开发人员可以使用“full = True”

于 2013-02-16T20:15:27.383 回答