在list_display中显示相册中的所有照片

时间:2015-01-28 22:38:43

标签: python django

我希望能够列出相册中的所有照片,但我不知道该怎么做。我目前收到错误:

AttributeError at /admin/portfolio/album/
'ImageFieldFile' object has no attribute 'title'

回溯:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  137.                 response = response.render()
File "/Library/Python/2.7/site-packages/django/template/response.py" in render
  103.             self.content = self.rendered_content
File "/Library/Python/2.7/site-packages/django/template/response.py" in rendered_content
  80.         content = template.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  148.             return self._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  126.         return compiled_parent._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
  142.         return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  844.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  80.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  1207.                     _dict = func(*resolved_args, **resolved_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
  316.             'results': list(results(cl))}
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in results
  292.             yield ResultList(None, items_for_result(cl, res, None))
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in __init__
  283.         super(ResultList, self).__init__(*items)
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in items_for_result
  198.             f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "/Library/Python/2.7/site-packages/django/contrib/admin/utils.py" in lookup_field
  280.                 value = attr()
File "/Users/bli1/Development/Django/Boothie/portfolio/models.py" in images
  22.         lst = [x.photo.title for x in self.photo_set.all()]

Exception Type: AttributeError at /admin/portfolio/album/
Exception Value: 'ImageFieldFile' object has no attribute 'title'

models.py:

class Album(models.Model):
    title = models.CharField(max_length=50, unique=True)
    # thumbnail = models.ImageField()

    def __str__(self):
        return self.title

    def images(self):
        lst = [x.photo.title for x in self.photo_set.all()]
        return lst

    def save(self, *args, **kwargs):
        # super(Album, self).save(*args, **kwargs)
        rgx = re.search(r'.*\w', self.title)
        self.title = rgx.group(0).replace(" ", "_")
        super(Album, self).save(*args, **kwargs)


class AlbumAdmin(admin.ModelAdmin):
    # Set search_fields to enable a search box on the admin change list page.
    # This should be set to a list of field names that will be searched whenever
    # somebody submits a search query in that text box.
    search_fields = ["title"]
    # Set list_display to control which fields are displayed on the change list page of the admin.
    # list_display = ["title", "images"]
    list_display = ["images"]


def upload_path(instance, filename):
    if " " in instance.album.title:
        instance.album.title.replace(" ", "_")
    return '/'.join([instance.album.title, filename])


class Photo(models.Model):
    title = models.CharField(max_length=50, blank=True)
    album = models.ForeignKey(Album)
    photo = models.ImageField(upload_to=upload_path)
    # width = models.IntegerField(blank=True, null=True)
    # height = models.IntegerField(blank=True, null=True)
    upload = models.DateTimeField(auto_now_add=True)
    # thumbnail = models.ImageField()

1 个答案:

答案 0 :(得分:1)

问题在于这一行:

lst = [x.photo.title for x in self.photo_set.all()]

self.photo_set.all()返回photo s的查询集。变量x是照片,x.photo是照片字段。因此,x.photo.title会引发AttributeError,因为该字段没有title属性。请尝试x.title

lst = [x.title for x in self.photo_set.all()]

如果要显示输出,最好返回字符串而不是列表:

titles = ", ".join(x.title for x in self.photo_set.all())
return titles