changeset 1037:7e0c3cbd3cda

Fix bad select_related call. In Django 1.8, select_related now throws an error if you give it an invalid field. This was happening. Fix that query. Also noticed an extra query generated in the display_post template. Fixed.
author Brian Neal <bgneal@gmail.com>
date Tue, 29 Dec 2015 22:21:42 -0600
parents b7b98c729abc
children 950dbf75634c
files forums/views/attachments.py sg101/templates/forums/display_post.html
diffstat 2 files changed, 12 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/forums/views/attachments.py	Sun Dec 27 12:00:51 2015 -0600
+++ b/forums/views/attachments.py	Tue Dec 29 22:21:42 2015 -0600
@@ -9,6 +9,7 @@
 from django.http import HttpResponseBadRequest
 from django.http import HttpResponseNotFound
 
+from forums.models import Attachment
 from forums.models import Post
 
 
@@ -30,8 +31,8 @@
     except Post.DoesNotExist:
         return HttpResponseNotFound("That post doesn't exist.")
 
-    embeds = post.attachments.all().select_related('embed')
-    data = [{'id': embed.id, 'html': embed.html} for embed in embeds]
+    attachments = Attachment.objects.filter(post=post).select_related('embed')
+    data = [{'id': a.embed.id, 'html': a.embed.html} for a in attachments]
 
     return HttpResponse(json.dumps(data), content_type='application/json')
 
--- a/sg101/templates/forums/display_post.html	Sun Dec 27 12:00:51 2015 -0600
+++ b/sg101/templates/forums/display_post.html	Tue Dec 29 22:21:42 2015 -0600
@@ -43,13 +43,15 @@
          <p class="small quiet">Last edited: {{ post.update_date|date:"M d, Y H:i:s" }}</p>
          {% endif %}
       </div>
-      {% if post.attachments.all %}
-      <div>
-         {% for item in post.attachments.all %}
-         <div class="forum-attachment">{{ item.html|safe }}</div>
-         {% endfor %}
-      </div>
-      {% endif %}
+      {% with attachments=post.attachments.all %}
+         {% if attachments %}
+         <div>
+            {% for item in attachments %}
+            <div class="forum-attachment">{{ item.html|safe }}</div>
+            {% endfor %}
+         </div>
+         {% endif %}
+      {% endwith %}
       <div class="forum-post-info-tools">
       <a href="#top" class="quiet" title="Goto the top of the page">Top</a>
       {% if can_reply %}