无法排除活塞中用户的外键字段
我有这个模型:
# models.py
from django.contrib.auth.models import User
class Test(models.Model):
author = models.ForeignKey(User, related_name="tests")
title = models.CharField(_("title"), max_length=100)
然后在 django 活塞 Web 服务的 api 文件夹中:
class TestHandler(BaseHandler):
allowed_methods = ("GET")
model = Test
fields = ("title", ("author", ("username",)))
def read(self, request, id):
base = self.model.objects
try:
r = base.get(pk=id)
return r
except:
return rc.NOT_FOUND
如果我调用此 Web 服务,那么我得到:
{
"title": "A test"
"author": {
"username": "menda",
"first_name": "",
"last_name": "",
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": "2011-02-09 10:39:02",
"password": "sha1$83f15$feb85449bdae1a55f3ad5b41a601dbdb35c844b7",
"email": "[email protected]",
"date_joined": "2011-02-02 10:49:48"
},
}
我也尝试使用排除,但它没有也不行。
如何仅获取 author
的用户名? 谢谢!
I have this model:
# models.py
from django.contrib.auth.models import User
class Test(models.Model):
author = models.ForeignKey(User, related_name="tests")
title = models.CharField(_("title"), max_length=100)
Then in the api
folder for the django piston webservice:
class TestHandler(BaseHandler):
allowed_methods = ("GET")
model = Test
fields = ("title", ("author", ("username",)))
def read(self, request, id):
base = self.model.objects
try:
r = base.get(pk=id)
return r
except:
return rc.NOT_FOUND
If I call this webservice then I get:
{
"title": "A test"
"author": {
"username": "menda",
"first_name": "",
"last_name": "",
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": "2011-02-09 10:39:02",
"password": "sha1$83f15$feb85449bdae1a55f3ad5b41a601dbdb35c844b7",
"email": "[email protected]",
"date_joined": "2011-02-02 10:49:48"
},
}
I also have tried to use exclude
, but it doesn't work either.
How can I get only the username for author
?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,问题是 Piston 使用的是另一个 Handler 类在 User 模型上定义的字段集,而不是此处指定的嵌套字段。
另一位用户在活塞讨论组上提到了完全相同的问题:
http: //groups.google.com/group/django-piston/browse_thread/thread/295de704615ee9bd
该问题显然是由 Piston 序列化代码中的错误引起的。
用文档的话来说:
这一切都很好,除了“(除非被覆盖。)”情况不会似乎没有正确处理。
我认为对emitters.py进行轻微修改可能会解决问题(第160-193行)...
应该(也许?)阅读
如果您决定尝试修补emitters.py,请告诉我如果这能解决问题——最好在 django-piston 中修复这个问题。
干杯!
Okay, so the problem is that Piston is using the set of fields defined on the User model by another Handler class, rather than the nested fields specified here.
Another user refers to the exact same issue on the piston discussion group here:
http://groups.google.com/group/django-piston/browse_thread/thread/295de704615ee9bd
The problem is apparently caused by bug in Piston's serialization code.
In the words of the documentation:
Which is all good, except that the "(unless overridden.)" case doesn't seem to be handled correctly.
I think that a slight modification in emitters.py might fix the issue (lines 160-193)...
Which should (perhaps?) read
If you do decide to try patching emitters.py let me know if that does the trick - it'd be good to get this patched in django-piston.
Cheers!
我认为你不必要地嵌套作者字段。
看起来您的 fields 属性应该改为:
来自活塞文档...
I think you're unnecessarily nesting the author field.
It looks like your fields attribute should instead read:
From the piston docs...