我们如何使用通用视图和django-mptt实现 CRUD 功能?我进行了很多搜索,但找不到单个教程/示例代码。
假设我们有一个课程层次结构或类别层次结构,或类似的东西......我们如何添加/删除/更新/读取它们?
例如我有这个模型:
from django.db import models
from mptt.models import MPTTModel , TreeForeignKey
class Genre(MPTTModel):
name = models.CharField(max_length = 50 , unique = True)
parent = TreeForeignKey('self' , null = True , blank = True , related_name = 'children')
class MPTTMeta:
order_insertion_by = ['name']
这个views.py:
from django.views.generic.list_detail import object_list
from mp.models import Genre
def genres_list(request):
''' Shows all of the genres '''
return object_list(request,
queryset = Genre.tree.all() ,
template_name = 'genres.html' ,
# template_object_name = 'nodes' ## Adding "nodes" variable didn't solve the problem
)
好吧...我收到此错误(错误在行号“5”中:{% recursetree nodes %}):
Caught VariableDoesNotExist while rendering: Failed lookup for key [nodes] in u"[{'paginator': None, 'is_paginated': False, 'page_obj': None, 'nodes_list': [<Genre: Genre object>, <Genre: Genre object>, <Genre: Genre object>, <Genre: Genre object>]}, {'csrf_token': <django.utils.functional.__proxy__ object at 0x7f5bb810f090>}, {'perms': <django.utils.functional.__proxy__ object at 0x7f5bb810ff10>, 'messages': <django.contrib.messages.storage.user_messages.LegacyFallbackStorage object at 0x324af50>, 'user': ....................................
<html>
2
3 {% load mptt_tags %}
4 <ul>
5 {% recursetree nodes %}
6 <li>
7 {{node.name}}