Django-piston:返回查询集和列表之间的区别?

发布于 2024-10-22 11:54:28 字数 2005 浏览 1 评论 0原文

我创建了这个简单的项目来说明我的问题。

这些是我的模型:

class Zoo(models.Model):
    name = models.CharField(max_length=30)

class Animal(models.Model):
    name = models.CharField(max_length=30)
    zoo = models.ForeignKey(Zoo)

    def speak(zelf):
        return 'woof woof'

这是我的基本处理程序:

class ZooHandler(BaseHandler):
    fields = ('id', 'name', 'speak')
    def read(self, request):
        z = Zoo.objects.get(pk=1)

        qs = z.animal_set.all()
        return qs

如果我不将查询集转换为列表,这是结果:

[
    {
        "id": 1, 
        "name": "Tiger", 
        "speak": "woof woof"
    }, 
    {
        "id": 2, 
        "name": "Panda", 
        "speak": "woof woof"
    }, 
    {
        "id": 3, 
        "name": "Bear", 
        "speak": "woof woof"
    }, 
    {
        "id": 4, 
        "name": "Parrot", 
        "speak": "woof woof"
    }, 
    {
        "id": 5, 
        "name": "Dolphin", 
        "speak": "woof woof"
    }
]

如果我将其转换为列表,就会发生这种情况 return list(qs)

[
    {
        "zoo_id": 1, 
        "_state": "<django.db.models.base.ModelState object at 0x2413f90>", 
        "id": 1, 
        "name": "Tiger"
    }, 
    {
        "zoo_id": 1, 
        "_state": "<django.db.models.base.ModelState object at 0x241d590>", 
        "id": 2, 
        "name": "Panda"
    }, 
    {
        "zoo_id": 1, 
        "_state": "<django.db.models.base.ModelState object at 0x241d6d0>", 
        "id": 3, 
        "name": "Bear"
    }, 
    {
        "zoo_id": 1, 
        "_state": "<django.db.models.base.ModelState object at 0x241d750>", 
        "id": 4, 
        "name": "Parrot"
    }, 
    {
        "zoo_id": 1, 
        "_state": "<django.db.models.base.ModelState object at 0x241d7d0>", 
        "id": 5, 
        "name": "Dolphin"
    }
]

我丢失了 say 方法结果,但获得了一个关系 id 和一个 _state 对象。谁能解释为什么会发生这种情况以及我如何防止它?这只是一个测试项目,我不想让任何人试图解释我的真实项目。

I created this simple project to illustrate my problem.

These are my models:

class Zoo(models.Model):
    name = models.CharField(max_length=30)

class Animal(models.Model):
    name = models.CharField(max_length=30)
    zoo = models.ForeignKey(Zoo)

    def speak(zelf):
        return 'woof woof'

This is my base handler:

class ZooHandler(BaseHandler):
    fields = ('id', 'name', 'speak')
    def read(self, request):
        z = Zoo.objects.get(pk=1)

        qs = z.animal_set.all()
        return qs

This is the result if I don't convert the queryset to a list:

[
    {
        "id": 1, 
        "name": "Tiger", 
        "speak": "woof woof"
    }, 
    {
        "id": 2, 
        "name": "Panda", 
        "speak": "woof woof"
    }, 
    {
        "id": 3, 
        "name": "Bear", 
        "speak": "woof woof"
    }, 
    {
        "id": 4, 
        "name": "Parrot", 
        "speak": "woof woof"
    }, 
    {
        "id": 5, 
        "name": "Dolphin", 
        "speak": "woof woof"
    }
]

This is what happens if I do convert it to a list return list(qs):

[
    {
        "zoo_id": 1, 
        "_state": "<django.db.models.base.ModelState object at 0x2413f90>", 
        "id": 1, 
        "name": "Tiger"
    }, 
    {
        "zoo_id": 1, 
        "_state": "<django.db.models.base.ModelState object at 0x241d590>", 
        "id": 2, 
        "name": "Panda"
    }, 
    {
        "zoo_id": 1, 
        "_state": "<django.db.models.base.ModelState object at 0x241d6d0>", 
        "id": 3, 
        "name": "Bear"
    }, 
    {
        "zoo_id": 1, 
        "_state": "<django.db.models.base.ModelState object at 0x241d750>", 
        "id": 4, 
        "name": "Parrot"
    }, 
    {
        "zoo_id": 1, 
        "_state": "<django.db.models.base.ModelState object at 0x241d7d0>", 
        "id": 5, 
        "name": "Dolphin"
    }
]

I lose the speak method result but gain a relational id and a _state object. Can anybody explain why this happens and how I can prevent it? This is just a test project I didn't wanna bore anybody trying to explain my real project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

在梵高的星空下 2024-10-29 11:54:28

因为您实际上不是序列化 Zoo 对象,而是序列化 Animal 对象。 Piston 看到您有一个 Animals 查询集,并尝试找到一个 Animal 处理程序 - 没有找到,它只是序列化所有内置对象,而不是自定义方法。

定义一个 AnimalHandler 类并将 fields 元组移动到那里,它应该可以工作。

Because you are not actually serializing Zoo objects, but Animal objects. Piston sees that you have a queryset of Animals, and tries to find an Animal handler - not finding one, it just serializes all the built-in objects, but not the custom method.

Define an AnimalHandler class and move the fields tuple to there, and it should work.

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