Django Piston:如何从处理程序结果中排除嵌套字段?有可能吗?
我正在对我为使用 django-piston 的 Django 应用程序编写的 API 进行最后的修改。该 API 能够通过请求或 IP 地址进行搜索,这些请求或 IP 地址分别是 Request
或 IPAddress
实例。每个请求可以有 1 个或多个与之关联的 IPAddress
。
例如,我有一个 API 调用,它将显示与活动状态“活动”、“非活动”或“全部”(任一状态)匹配的所有 IPAddress
对象。每个 IPAddress
实例关联的 Request
可用作 IPAddress.request
。
我遇到的问题是 Request.inputter
是提供请求的人的 User
实例的外键。当我为此 API 调用创建的处理程序返回结果时,将显示 User
实例中的所有字段,包括 password
。
这很糟糕;我不想要这个。
这是我的处理程序:
class SearchByIPStatusHandler(BaseHandler):
model = IPAddress
allowed_methods = ('GET',)
anonymous = AnonymousIPHandler
def read(self, request, status):
"""
Returns IP addresses based on activity status.
Status: 'active', 'inactive', 'all'
"""
if status == 'all':
return self.model.objects.all()
else:
active = True if (status=='active') else False
return self.model.objects.filter(active=active)
这是来自 /api/show/all/
的结果示例:
<response>
<resource>
<updated>2010-02-05 17:08:53.651729</updated>
<expires>2010-02-12 17:08:23</expires>
<created>2010-02-05 17:08:53.625318</created>
<nexthop>255.255.255.255</nexthop>
<netmask>255.255.255.254</netmask>
<address>2.4.6.80/31</address>
<active>True</active>
<id>4</id>
<request>
<updated>2010-02-05 17:08:53.382381</updated>
<created>2010-02-05 17:08:53.382313</created>
<expires>2010-02-12 17:08:23</expires>
<incident>20100212-badthings-01</incident>
<reason>bad things happened</reason>
<inputter>
<username>jathan</username>
<first_name>Jathan</first_name>
<last_name>McCollum</last_name>
<is_active>True</is_active>
<email>[email protected]</email>
<is_superuser>True</is_superuser>
<is_staff>True</is_staff>
<last_login>2010-02-05 18:55:51.877746</last_login>
<password>[ENCRYPTED STRING I REDACTED]</password>
<id>1</id>
<date_joined>2010-01-28 09:56:32</date_joined>
</inputter>
<requester>joeuser</requester>
<active>True</active>
</request>
</resource>
</response>
我真正想要的结果是 inputter.username
,不是所有其他的东西。我尝试了在处理程序上实现排除属性的各种迭代,但均无济于事。如果我只是跳过整个请求字段,那就可以正常工作,如下所示:
In handler:
exclude = ('request', )
其结果是:
<response>
<resource>
<updated>2010-02-05 17:08:53.651729</updated>
<expires>2010-02-12 17:08:23</expires>
<created>2010-02-05 17:08:53.625318</created>
<nexthop>255.255.255.255</nexthop>
<netmask>255.255.255.254</netmask>
<address>2.4.6.80/31</address>
<active>True</active>
<id>4</id>
</resource>
</response>
但这些结果也不是我想要的。
所以,最后,我的问题是:
如何从处理程序结果中排除嵌套字段?它甚至可能吗?
我已经尝试了以下各种迭代,所有这些迭代要么没有结果,要么没有预期的结果:
# try to exclude request.inputter
exclude = ( ('request', ('inputter', ), ) )
# try to exclude request.inputter.password
exclude = ( ('request', ('inputter', ('password', ) ) ) )
我认为我误解或滥用了在此上下文中完成字段排除的方式,因此任何非常感谢对此主题的启发。
I am putting the finishing touches on an API I have written for a Django app utilizing django-piston. The API is able to search by request or IP address which are Request
or IPAddress
instances respectively. Each request can have 1 or more IPAddress
associated with it.
So, for example I have an API call that will show all IPAddress
objects matching an activity status of "active", "inactive", or "all" (for either). The Request
to which each IPAddress
instance is associated is available as IPAddress.request
.
The problem I am having is that Request.inputter
is a foreign key to the User
instance of the person who provisioned the request. When my results are returned from the handler I have created for this API call, all fields from the User
instance are displayed, including password
.
This is bad; I do not want this.
So here is my handler:
class SearchByIPStatusHandler(BaseHandler):
model = IPAddress
allowed_methods = ('GET',)
anonymous = AnonymousIPHandler
def read(self, request, status):
"""
Returns IP addresses based on activity status.
Status: 'active', 'inactive', 'all'
"""
if status == 'all':
return self.model.objects.all()
else:
active = True if (status=='active') else False
return self.model.objects.filter(active=active)
And here is an example of the results from /api/show/all/
:
<response>
<resource>
<updated>2010-02-05 17:08:53.651729</updated>
<expires>2010-02-12 17:08:23</expires>
<created>2010-02-05 17:08:53.625318</created>
<nexthop>255.255.255.255</nexthop>
<netmask>255.255.255.254</netmask>
<address>2.4.6.80/31</address>
<active>True</active>
<id>4</id>
<request>
<updated>2010-02-05 17:08:53.382381</updated>
<created>2010-02-05 17:08:53.382313</created>
<expires>2010-02-12 17:08:23</expires>
<incident>20100212-badthings-01</incident>
<reason>bad things happened</reason>
<inputter>
<username>jathan</username>
<first_name>Jathan</first_name>
<last_name>McCollum</last_name>
<is_active>True</is_active>
<email>[email protected]</email>
<is_superuser>True</is_superuser>
<is_staff>True</is_staff>
<last_login>2010-02-05 18:55:51.877746</last_login>
<password>[ENCRYPTED STRING I REDACTED]</password>
<id>1</id>
<date_joined>2010-01-28 09:56:32</date_joined>
</inputter>
<requester>joeuser</requester>
<active>True</active>
</request>
</resource>
</response>
All I really want in the results is the inputter.username
, not all of the other stuff. I have tried various iterations of implementing an exclude
attribute on the handler to no avail. If I just skip the entire request field, that works fine, like so:
In handler:
exclude = ('request', )
Which results in:
<response>
<resource>
<updated>2010-02-05 17:08:53.651729</updated>
<expires>2010-02-12 17:08:23</expires>
<created>2010-02-05 17:08:53.625318</created>
<nexthop>255.255.255.255</nexthop>
<netmask>255.255.255.254</netmask>
<address>2.4.6.80/31</address>
<active>True</active>
<id>4</id>
</resource>
</response>
But these results are also not what I want.
So, finally, my question:
How can I exclude nested fields from handler results? Is it even possible?
I have tried various iterations of the following, all of which either have no result, or unintended results:
# try to exclude request.inputter
exclude = ( ('request', ('inputter', ), ) )
# try to exclude request.inputter.password
exclude = ( ('request', ('inputter', ('password', ) ) ) )
I assume that I am misunderstanding or misusing the way field exclusions are done in this context, so any enlightenment on this topic is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过处理程序的
fields =
子句指定所需的字段来获得所需的结果。来自外键的模型字段可以这样指定:
在您的情况下,以下内容应该有效(为了简洁起见,省略了一些字段):
You can get the desired result by specifying the desired fields via the handler's
fields =
clause.Model fields coming from foreign keys can be specified like this:
In your case, the following should work (some of your fields left out for brevity):
您可以尝试使用包含而不是排除吗?例如,
我不记得我编写的
exclude
是否与include
一样通用。另外,django-piston Google 小组是我们讨论大多数事情的地方,您在那里提出这个问题可能会更成功。
Can you try using include instead of exclude? E.g.
I don't remember if I wrote
exclude
to be as versatile asinclude
.Also, the django-piston Google group is where we discuss most things, you may have more success asking this question there.