django-mptt视图使用jstree
练习目的 掌握jstree和django-mptt的使用
关健代码
def genre_jstree(root, data=dict):
if hasattr(data, '__call__'):
data = data()
data['text'] = root.name
children = root.children.all()
if len(children) > 0:
data['children'] = []
for n in children:
data['children'].append(genre_jstree(n))
return data
def genre_jstree_v(request):
root = Genre.objects.first().get_root()
return JsonResponse(genre_jstree(root))