无法排除活塞中用户的外键字段

发布于 2024-10-16 20:59:53 字数 1263 浏览 3 评论 0原文

我有这个模型:

# 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心安伴我暖 2024-10-23 20:59:53

好的,问题是 Piston 使用的是另一个 Handler 类在 User 模型上定义的字段集,而不是此处指定的嵌套字段。

另一位用户在活塞讨论组上提到了完全相同的问题:

http: //groups.google.com/group/django-piston/browse_thread/thread/295de704615ee9bd

该问题显然是由 Piston 序列化代码中的错误引起的。
用文档的话来说:

通过在处理程序中使用模型,Piston 将记住您的字段/排除指令,并在返回该类型对象的其他处理程序中使用它们(除非被覆盖。)

这一切都很好,除了“(除非被覆盖。)”情况不会似乎没有正确处理。

认为对emitters.py进行轻微修改可能会解决问题(第160-193行)...

if handler:
    fields = getattr(handler, 'fields')                    
if not fields or hasattr(handler, 'fields'):
    ...dostuff...
else:
    get_fields = set(fields)

应该(也许?)阅读

if fields:
    get_fields = set(fields)
else:
    if handler:
        fields = getattr(handler, 'fields')
    ...dostuff...

如果您决定尝试修补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:

By using a model in a handler, Piston will remember your fields/exclude directives and use them in other handlers who return objects of that type (unless overridden.)

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)...

if handler:
    fields = getattr(handler, 'fields')                    
if not fields or hasattr(handler, 'fields'):
    ...dostuff...
else:
    get_fields = set(fields)

Which should (perhaps?) read

if fields:
    get_fields = set(fields)
else:
    if handler:
        fields = getattr(handler, 'fields')
    ...dostuff...

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!

┊风居住的梦幻卍 2024-10-23 20:59:53

我认为你不必要地嵌套作者字段。

看起来您的 fields 属性应该改为:

fields = ("title", "author", ("username",))

来自活塞文档...

class UserHandler(BaseHandler):
      model = User
      fields = ('name', 'posts', ('title', 'date'))

will show the title and date from a users posts.

I think you're unnecessarily nesting the author field.

It looks like your fields attribute should instead read:

fields = ("title", "author", ("username",))

From the piston docs...

class UserHandler(BaseHandler):
      model = User
      fields = ('name', 'posts', ('title', 'date'))

will show the title and date from a users posts.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文