Merge lp:~fo0bar/turku/turku-api-related-links into lp:turku/turku-api

Proposed by Ryan Finnie
Status: Merged
Approved by: Joel Sing
Approved revision: 63
Merged at revision: 64
Proposed branch: lp:~fo0bar/turku/turku-api-related-links
Merge into: lp:turku/turku-api
Diff against target: 45 lines (+15/-8)
2 files modified
turku_api/admin.py (+12/-5)
turku_api/templates/admin/custom_change_form.html (+3/-3)
To merge this branch: bzr merge lp:~fo0bar/turku/turku-api-related-links
Reviewer Review Type Date Requested Status
Joel Sing (community) +1 Approve
Review via email: mp+382103@code.launchpad.net

Commit message

Fix admin child object related links

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Joel Sing (jsing) wrote :

LGTM, thanks.

review: Approve (+1)
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 64

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'turku_api/admin.py'
--- turku_api/admin.py 2020-03-24 23:07:22 +0000
+++ turku_api/admin.py 2020-04-11 22:36:45 +0000
@@ -58,12 +58,19 @@
58 change_form_template = 'admin/custom_change_form.html'58 change_form_template = 'admin/custom_change_form.html'
5959
60 def render_change_form(self, request, context, *args, **kwargs):60 def render_change_form(self, request, context, *args, **kwargs):
61 # Build a list of related children objects and their counts
62 # so they may be linked to in the admin interface
61 related_links = []63 related_links = []
62 # Broken in Django 1.8+; see if related_links can be re-implemented64 if 'object_id' in context and hasattr(self.model._meta, 'get_fields'):
63 #if 'object_id' in context:65 related_objs = [
64 # for obj in self.model._meta.get_all_related_objects():66 f for f in self.model._meta.get_fields()
65 # count = obj.model.objects.filter(**{obj.field.name: context['object_id']}).count()67 if (f.one_to_many or f.one_to_one)
66 # related_links.append((obj, count))68 and f.auto_created and not f.concrete
69 ]
70 for obj in related_objs:
71 count = obj.related_model.objects.filter(**{obj.field.name: context['object_id']}).count()
72 if count > 0:
73 related_links.append((obj, obj.related_model._meta, count))
67 context.update({'related_links': related_links})74 context.update({'related_links': related_links})
6875
69 return super(CustomModelAdmin, self).render_change_form(request, context, *args, **kwargs)76 return super(CustomModelAdmin, self).render_change_form(request, context, *args, **kwargs)
7077
=== modified file 'turku_api/templates/admin/custom_change_form.html'
--- turku_api/templates/admin/custom_change_form.html 2015-03-16 02:34:28 +0000
+++ turku_api/templates/admin/custom_change_form.html 2020-04-11 22:36:45 +0000
@@ -3,9 +3,9 @@
3{% if related_links %}3{% if related_links %}
4<h2>Related</h2>4<h2>Related</h2>
5<ul>5<ul>
6{% for link_obj, link_count in related_links %}6{% for link_obj, link_opts, link_count in related_links %}
7 {% url 'admin:'|add:app_label|add:'_'|add:link_obj.opts.model_name|add:'_changelist' as link_url %}7 {% url 'admin:'|add:app_label|add:'_'|add:link_opts.model_name|add:'_changelist' as link_url %}
8 <li><a href="{{ link_url }}?{{ link_obj.field.name }}={{ object_id }}">{{ link_obj.opts.verbose_name_plural|capfirst|escape }} ({{ link_count }})</a></li>8 <li><a href="{{ link_url }}?{{ link_obj.field.name }}={{ object_id }}">{{ link_opts.verbose_name_plural|capfirst|escape }} ({{ link_count }})</a></li>
9{% endfor %}9{% endfor %}
10</ul>10</ul>
11{% endif %}11{% endif %}

Subscribers

People subscribed via source and target branches

to all changes: