У меня есть следующая модель:
class Performance(Event):
date_created = models.DateTimeField(_('date created'), default=timezone.now)
date_modified = models.DateTimeField(_('date_modified'), auto_now=True)
class PerformanceWork(models.Model):
perf = models.ForeignKey(Performance, related_name='perf_work', blank=True, null=True, on_delete=models.CASCADE)
work = models.ForeignKey(WorkMusic, related_name='work', blank=True, null=True, on_delete=models.CASCADE)
class WorkMusic(MPTTModel, Work):
...
В моем view.py:
class PerformanceView(TemplateView):
template_name = 'performances/performance.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'perf': Performance.objects.get(pk=self.kwargs['pk'])
})
return context
В моем шаблоне я пытаюсь отобразить рекурсивное дерево с этим в моем шаблоне :
{% for work in perf.perf_work.all %}
{% recursetree work.work %}
<li>
{% if node.is_leaf_node %}
{{ node.name_original }}
{% endif %}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
{% endfor %}
При запуске моего приложения я получаю следующую ошибку:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/performance/2/
Django Version: 3.0
Python Version: 3.7.5
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'opus',
'generic',
'art_perf',
'art_visual',
'debug_toolbar',
'mptt',
'import_export',
'googlemaps']
Installed Middleware:
('whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware')
Template error:
In template /Users/howiec/Desktop/Opus/templates/performances/base.html, error at line 0
'WorkMusic' object is not subscriptable
1 : {% load static i18n %}
2 :
3 : <!DOCTYPE html>
4 : <html lang="en" class="{% block html_class %}{% endblock %}">
5 : <head>
6 : <meta charset="UTF-8">
7 : <meta name="viewport" content="width=device-width, initial-scale=1.0">
8 : <meta http-equiv="X-UA-Compatible" content="ie=edge">
9 :
10 : <link rel="stylesheet" type="text/css" href="{% static 'css/performance.css' %}">
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 143, in _get_response
response = response.render()
File "/usr/local/lib/python3.7/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/usr/local/lib/python3.7/site-packages/django/template/response.py", line 83, in rendered_content
return template.render(context, self._request)
File "/usr/local/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/usr/local/lib/python3.7/site-packages/django/test/utils.py", line 95, in instrumented_test_render
return self.nodelist.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/usr/local/lib/python3.7/site-packages/django/test/utils.py", line 95, in instrumented_test_render
return self.nodelist.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.7/site-packages/django/template/defaulttags.py", line 209, in render
nodelist.append(node.render_annotated(context))
File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/usr/local/lib/python3.7/site-packages/mptt/templatetags/mptt_tags.py", line 257, in render
roots = cache_tree_children(queryset)
File "/usr/local/lib/python3.7/site-packages/mptt/templatetags/mptt_tags.py", line 236, in cache_tree_children
return get_cached_trees(queryset)
File "/usr/local/lib/python3.7/site-packages/mptt/utils.py", line 232, in get_cached_trees
parent_attr = queryset[0]._mptt_meta.parent_attr
Exception Type: TypeError at /performance/2/
Exception Value: 'WorkMusic' object is not subscriptable
Work.work - это набор запросов с объектом WorkMusi c:
Я знаю, что это можно понять, когда я просто графически отображаю объект WorkMusi c. Я не уверен, почему этот разрыв, когда я получаю доступ к объекту через другую модель?