Django-piston:返回查询集和列表之间的区别?
我创建了这个简单的项目来说明我的问题。
这些是我的模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您实际上不是序列化 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 thefields
tuple to there, and it should work.