Gradding a random image causes the title, url (etc) attributes on the tag.rand_img to vanish, meaning I can't display anything more than the object name.
view.py:
def locations_gallery(request):
queryset = Photo.objects.all()
tags = Tag.objects.usage_for_queryset(queryset, counts=True)
tags.sort(key=operator.attrgetter('count'), reverse=True)
for tag in tags:
image_tag = Tag.objects.get(name=tag)
tag.rand_img = TaggedItem.objects.get_union_by_model(Photo, image_tag).order_by('?')[:1]
ctxt['tags'] = tags
return render_to_response('locations_gallery.html', ctxt, RequestContext(request))
Template (simplified):
{% for tag in tags %}
{{ tag.name }}
<img src="{{ tag.rand_img.url }}">
{% endfor %}
This line:
is returning a Queryset, not a model instance. The queryset doesn't have your custom methods, they exist only on the instance within that queryset.
Replace the
[:1]
with[0]
to get the actual model instance (you'll need to catchIndexError
in case there isn't a matching item).