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 +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 Mergebot (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 Mergebot (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
1=== modified file 'turku_api/admin.py'
2--- turku_api/admin.py 2020-03-24 23:07:22 +0000
3+++ turku_api/admin.py 2020-04-11 22:36:45 +0000
4@@ -58,12 +58,19 @@
5 change_form_template = 'admin/custom_change_form.html'
6
7 def render_change_form(self, request, context, *args, **kwargs):
8+ # Build a list of related children objects and their counts
9+ # so they may be linked to in the admin interface
10 related_links = []
11- # Broken in Django 1.8+; see if related_links can be re-implemented
12- #if 'object_id' in context:
13- # for obj in self.model._meta.get_all_related_objects():
14- # count = obj.model.objects.filter(**{obj.field.name: context['object_id']}).count()
15- # related_links.append((obj, count))
16+ if 'object_id' in context and hasattr(self.model._meta, 'get_fields'):
17+ related_objs = [
18+ f for f in self.model._meta.get_fields()
19+ if (f.one_to_many or f.one_to_one)
20+ and f.auto_created and not f.concrete
21+ ]
22+ for obj in related_objs:
23+ count = obj.related_model.objects.filter(**{obj.field.name: context['object_id']}).count()
24+ if count > 0:
25+ related_links.append((obj, obj.related_model._meta, count))
26 context.update({'related_links': related_links})
27
28 return super(CustomModelAdmin, self).render_change_form(request, context, *args, **kwargs)
29
30=== modified file 'turku_api/templates/admin/custom_change_form.html'
31--- turku_api/templates/admin/custom_change_form.html 2015-03-16 02:34:28 +0000
32+++ turku_api/templates/admin/custom_change_form.html 2020-04-11 22:36:45 +0000
33@@ -3,9 +3,9 @@
34 {% if related_links %}
35 <h2>Related</h2>
36 <ul>
37-{% for link_obj, link_count in related_links %}
38- {% url 'admin:'|add:app_label|add:'_'|add:link_obj.opts.model_name|add:'_changelist' as link_url %}
39- <li><a href="{{ link_url }}?{{ link_obj.field.name }}={{ object_id }}">{{ link_obj.opts.verbose_name_plural|capfirst|escape }} ({{ link_count }})</a></li>
40+{% for link_obj, link_opts, link_count in related_links %}
41+ {% url 'admin:'|add:app_label|add:'_'|add:link_opts.model_name|add:'_changelist' as link_url %}
42+ <li><a href="{{ link_url }}?{{ link_obj.field.name }}={{ object_id }}">{{ link_opts.verbose_name_plural|capfirst|escape }} ({{ link_count }})</a></li>
43 {% endfor %}
44 </ul>
45 {% endif %}

Subscribers

People subscribed via source and target branches

to all changes: