【发布时间】:2015-01-23 00:34:49
【问题描述】:
大家!
我是 Django(和 Tastypie)的新手,我在分层数据方面遇到了一些问题。在我的项目中,我们有几个类别,由以下模型表示:
class Category(MPPTModel):
desc = models.CharField(max_length=200)
parent = TreeForeignKey('self', null=True, related_name='children')
至于服务器响应,我想要类似的东西(一个包含所有类别及其各自子类别的 json,等等):
[
{
"id" : 0,
"decription" : "category1",
"categories" : [
{
"id" : 1,
"description" : "category2",
"categories" : [ ... ]
},
...
]
},
...
]
资源模型:
class CategoryResource(ModelResource):
resource_name = 'listCategories'
queryset = Category.objects.filter.all()
def alter_list_data_to_serialize(self, request, data):
return { 'status' : 'success', 'categories' : data }
我尝试过对 Paginator 进行子类化,但我注意到我只能在“对象”列表中发送纯数据。有没有办法修改这种行为?我在这里监督什么吗?
感谢您的宝贵时间。
【问题讨论】:
标签: python django tastypie hierarchical django-mptt