0
everyone!在Tastypie中表示分层数据
我是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" : [ ... ]
},
...
]
},
...
]
的ResourceModel:
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 }
我试过子类分页程序,但我注意到,我只允许发送的“对象”名单内明文数据。有什么方法可以修改这种行为吗?我在这里监督什么?
谢谢你的时间。
它帮助了我! :)。如果你想让你的父母在根目录下,并且在你的回复中有孩子,你也可以使用fields.ToManyField('self','children',full = True,null = True)。 – Deepak 2017-10-05 16:06:18