comparison forums/views/attachments.py @ 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 89b240fe9297
children
comparison
equal deleted inserted replaced
1036:b7b98c729abc 1037:7e0c3cbd3cda
7 from django.http import HttpResponse 7 from django.http import HttpResponse
8 from django.http import HttpResponseForbidden 8 from django.http import HttpResponseForbidden
9 from django.http import HttpResponseBadRequest 9 from django.http import HttpResponseBadRequest
10 from django.http import HttpResponseNotFound 10 from django.http import HttpResponseNotFound
11 11
12 from forums.models import Attachment
12 from forums.models import Post 13 from forums.models import Post
13 14
14 15
15 def fetch_attachments(request): 16 def fetch_attachments(request):
16 """ 17 """
28 try: 29 try:
29 post = Post.objects.get(pk=post_id) 30 post = Post.objects.get(pk=post_id)
30 except Post.DoesNotExist: 31 except Post.DoesNotExist:
31 return HttpResponseNotFound("That post doesn't exist.") 32 return HttpResponseNotFound("That post doesn't exist.")
32 33
33 embeds = post.attachments.all().select_related('embed') 34 attachments = Attachment.objects.filter(post=post).select_related('embed')
34 data = [{'id': embed.id, 'html': embed.html} for embed in embeds] 35 data = [{'id': a.embed.id, 'html': a.embed.html} for a in attachments]
35 36
36 return HttpResponse(json.dumps(data), content_type='application/json') 37 return HttpResponse(json.dumps(data), content_type='application/json')
37 38