Skip to content

Commit 25806c1

Browse files
committed
Remove models from children context
Before this, we generated the list of child classes based on a list of model instances. We now create a simple python object, and pass that into the template context instead.
1 parent 187a8b6 commit 25806c1

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

cbv/templates/cbv/klass_detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ <h2>Ancestors (<abbr title="Method Resolution Order">MRO</abbr>)</h2>
8787
<h2>Descendants</h2>
8888
<ul class="unstyled">
8989
{% for child in all_children %}
90-
<li><a href="{{ child.get_absolute_url }}">{{ child.name }}</a></li>
90+
<li><a href="{{ child.url }}">{{ child.name }}</a></li>
9191
{% endfor %}
9292
</ul>
9393
</div>

cbv/views.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Ancestor:
2727
url: str
2828
is_direct: bool
2929

30+
@attrs.frozen
31+
class Child:
32+
name: str
33+
url: str
34+
3035
def get_context_data(self, **kwargs):
3136
qs = Klass.objects.filter(
3237
name__iexact=self.kwargs["klass"],
@@ -60,9 +65,16 @@ def get_context_data(self, **kwargs):
6065
)
6166
for ancestor in klass.get_all_ancestors()
6267
]
68+
children = [
69+
self.Child(
70+
name=child.name,
71+
url=child.get_absolute_url(),
72+
)
73+
for child in klass.get_all_children()
74+
]
6375
return {
6476
"all_ancestors": ancestors,
65-
"all_children": list(klass.get_all_children()),
77+
"all_children": children,
6678
"attributes": klass.get_prepared_attributes(),
6779
"canonical_url": self.request.build_absolute_uri(canonical_url_path),
6880
"direct_ancestors": direct_ancestors,

0 commit comments

Comments
 (0)