我有这个模型:
# 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)
然后在api
django 活塞网络服务的文件夹中:
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
如果我调用这个网络服务,那么我会得到:
{
"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": "b@a.as",
"date_joined": "2011-02-02 10:49:48"
},
}
我也尝试过使用exclude
,但它也不起作用。
我怎样才能只获得用户名author
?谢谢!