当只允许一个时,list_display函数传递2个参数

时间:2015-02-05 09:13:40

标签: python django

我不明白为什么会收到此错误:

TypeError at /admin/portfolio/photo/
photo_display() takes exactly 1 argument (2 given)

我的photo_display()函数只接受一个参数。我不确定两个论点的传递方式和方式。

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

    def __str__(self):
        return self.title

    class PhotoAdmin(admin.ModelAdmin):
        search_fields = ["title", "photo"]
        list_display = ["photo_display", "thumbnail", "title", "album", "size"]
        list_filter = ["album"]

        def size(self):
            # Photo size
            return "%s x %s" % (self.width, self.height)

        def thumbnail(self):
            thumbnail_html = "<a href=\"{0}{1}\"><img border=\"0\" alt=\"\" src=\"{2}{3}\" height=\"80\" /></a>".format(settings.MEDIA_URL, self.name, settings.MEDIA_URL, self.name)
            return thumbnail_html

        thumbnail.allow_tags = True

        def photo_name(self):
            return os.path.basename(settings.MEDIA_ROOT + "/" + self.name)

        def photo_display(self):
            return mark_safe('<a href="%s">%s</a>' % (self.photo.url, os.path.split(self.photo.name)[1]))

回溯:

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
  276.             value = attr(obj)

Exception Type: TypeError at /admin/portfolio/photo/
Exception Value: photo_display() takes exactly 1 argument (2 given)

1 个答案:

答案 0 :(得分:4)

self中的

photo_displayModelAdmin个实例。您应该为Photo实例添加the second argument

def photo_display(self, obj):
    return u'<a href="%s">%s</a>' % (obj.photo.url,
                                     os.path.split(obj.photo.name)[1])
photo_display.allow_tags = True