id summary reporter owner description type status component version severity resolution keywords cc stage has_patch needs_docs needs_tests needs_better_patch easy ui_ux
13848 Template filter bug when accessing foo_set manager. danols@… nobody "Hi Awesome Django Team,
Below is a bug that I've discovered (I did not find anything similiar in the bug list). The best way I understand it is that when using foo.bar_set.all.0 with a filter the filter is called twice. First with the bar object being passed and then second with the bar object as string.
Below is the code to duplicate it:
{{{
class Foo(models.Model):
title = models.CharField(max_length=50)
class Photo(models.Model):
image = models.ImageField(upload_to='')
}}}
In a view I pass all Foos objects to the template. And attempt to retrieve just the first image to be displayed but first it is passed to a filter to create a thubmnail.
Here is the rough template code that generates the error: Caught AttributeError while rendering: 'str' object has no attribute 'path'.
{{{
{% for foo in foos %}
}}}
A workarround is insert another loop like this:
{{{
{% for foo in foos %}
{% for photo in foo.photo_set.all %}
{% if forloop.first %}
#
#
# not implemented os.path.getmtime(miniature_filename):
os.unlink(miniature_filename)
# if the image wasn't already resized, resize it
if not os.path.exists(miniature_filename):
image = Image.open(filename)
# crop if specified.
if (False):
# find out the x1,x2,y1,y2 cordinates for the croping of the image
# we are going to center this ractangel.
int_width = image.size[0]
int_height = image.size[1]
# find out biggest square size
int_box_side = 0
if int_width < int_height:
int_box_side = int_width
else:
int_box_side = int_height
# fidn out the offset
int_width_offset = (int_width - int_box_side) / 2 # since two sides
int_height_offset = (int_height - int_box_side) / 2
# crop it
image = image.crop([int_width_offset,int_height_offset,int_width-int_width_offset,int_height-int_height_offset])
image.thumbnail([x, y], Image.ANTIALIAS)
try:
image.save(miniature_filename, image.format, quality=90, optimize=1)
except:
image.save(miniature_filename, image.format, quality=90)
return miniature_url
register.filter(thumbnail)
}}}" closed Template system 1.2 worksforme Related objects Unreviewed 0 0 0 0 0 0