我有這樣的模式:不能排除ForeignKey的領域爲用戶在活塞
# 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"
},
}
我也試過用exclude
,但它也不管用。
我怎樣才能只爲author
的用戶名? 謝謝!
你的代碼看起來我的權利。其實,我知道,這將是令人沮喪,但我確切地複製你的榜樣,而我只得到了作者的用戶名,並在響應標題,完全按照自己的意圖。其餘用戶信息不顯示。 (你有嵌套是正確的,順便說一句。) – mjjohnson 2011-02-09 18:26:56