Django Admin:TypeError:__str__返回非字符串(类型FieldFile)

时间:2015-06-19 17:59:24

标签: python django

我有一个正常运行的Django站点和一个正常运行的管理员,除非我尝试使用管理站点从模型“文章”中创建一个新对象。当我尝试创建一个新的“文章”时,我收到了这个错误:

TypeError at /admin/blog/article/add/

__str__ returned non-string (type FieldFile)
Exception Type:     TypeError
Exception Value:    __str__ returned non-string (type FieldFile)
Exception Location:     /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/utils/encoding.py in force_text, line 90

这是奇怪的事情:今天早上工作了。我尝试安装django-markdown,我开始收到此错误。我无法让它工作,所以我卸载了django-markdown并将所有代码恢复为原始代码(之前的功能)并进行了适当的迁移。

但我仍然收到错误。

这是追溯和渲染错误:

Error during template rendering

In template /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19
__str__ returned non-string (type FieldFile)
9               {% for field in line %}
10                  <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>
11                      {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
12                      {% if field.is_checkbox %}
13                          {{ field.field }}{{ field.label_tag }}
14                      {% else %}
15                          {{ field.label_tag }}
16                          {% if field.is_readonly %}
17                              <p>{{ field.contents }}</p>
18                          {% else %}
19  

      {{ field.field }}



20                          {% endif %}
21                      {% endif %}
22                      {% if field.field.help_text %}
23                          <p class="help">{{ field.field.help_text|safe }}</p>
24                      {% endif %}
25                  </div>
26              {% endfor %}
27          </div>
28      {% endfor %}
29  </fieldset>
Traceback Switch to copy-and-paste view

    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/core/handlers/base.py in get_response

                                    response = response.render()

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/response.py in render

                                self.content = self.rendered_content

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/response.py in rendered_content

                            content = template.render(context, self._request)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/backends/django.py in render

                            return self.template.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render

                                        return self._render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in _render

                            return self.nodelist.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render

                                    bit = self.render_node(node, context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node

                                return node.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/loader_tags.py in render

                            return compiled_parent._render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in _render

                            return self.nodelist.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render

                                    bit = self.render_node(node, context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node

                                return node.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/loader_tags.py in render

                            return compiled_parent._render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in _render

                            return self.nodelist.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render

                                    bit = self.render_node(node, context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node

                                return node.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/loader_tags.py in render

                                    result = block.nodelist.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render

                                    bit = self.render_node(node, context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node

                                return node.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/loader_tags.py in render

                                    result = block.nodelist.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render

                                    bit = self.render_node(node, context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node

                                return node.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/defaulttags.py in render

                                                nodelist.append(node.render(context))

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/loader_tags.py in render

                                    return template.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render

                                    return self._render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in _render

                            return self.nodelist.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render

                                    bit = self.render_node(node, context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node

                                return node.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/defaulttags.py in render

                                                nodelist.append(node.render(context))

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/defaulttags.py in render

                                                nodelist.append(node.render(context))

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/defaulttags.py in render

                                    return nodelist.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render

                                    bit = self.render_node(node, context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node

                                return node.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/defaulttags.py in render

                                    return nodelist.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/base.py in render

                                    bit = self.render_node(node, context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render_node

                                return node.render(context)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/template/debug.py in render

                                output = force_text(output)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/utils/encoding.py in force_text

                                        s = six.text_type(s)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/utils/html.py in <lambda>

                            klass.__str__ = lambda self: mark_safe(klass_str(self))

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/forms.py in __str__

                            return self.as_widget()

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/forms.py in as_widget

                            return force_text(widget.render(name, self.value(), attrs=attrs))

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/contrib/admin/widgets.py in render

                                'widget': self.widget.render(name, value, *args, **kwargs),

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/widgets.py in render

                            options = self.render_options(choices, value)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/widgets.py in render_options

                            for option_value, option_label in chain(self.choices, choices):

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/models.py in __iter__

                                    yield self.choice(obj)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/models.py in choice

                            return (self.field.prepare_value(obj), self.field.label_from_instance(obj))

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/forms/models.py in label_from_instance

                            return smart_text(obj)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/utils/encoding.py in smart_text

                        return force_text(s, encoding, strings_only, errors)

         ...
    ▶ Local vars
    /home/USERNAME/webapps/APPNAME/lib/python3.4/Django-1.8.2-py3.4.egg/django/utils/encoding.py in force_text

                                        s = six.text_type(s)

         ...
    ▶ Local vars 

这是我的文章模型:

class Article(models.Model):
    author = models.ForeignKey(Author, blank=True, null=True)
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, blank=True, null=True)
    article_category = models.OneToOneField(ArticleCategory, blank=True, null=True)
    body_en = models.TextField(blank=True, null=True)
    body_fr = models.TextField(blank=True, null=True)
    is_review = models.BooleanField(default=False)
    publish = models.BooleanField(default=True)
    date_created = models.DateTimeField(blank=True, null=True)
    modified = models.DateTimeField(auto_now=True)
    tags = models.ManyToManyField(Tag, blank=True, null=True)
    work_link = models.ForeignKey(Work, null=True, blank=True)
    artist_link = models.ForeignKey(Artist, null=True, blank=True)
    images = models.ManyToManyField(ArticleImage)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ["-date_created"]

admin.py:

from django.contrib import admin
from blog.models import Article, Tag, Author, ArticleCategory, ArticleImage

# Register your models here.
admin.site.register(Article)
admin.site.register(Author)
admin.site.register(Tag)
admin.site.register(ArticleCategory)
admin.site.register(ArticleImage)

我几乎想要卸载,然后只是使用当前的设置重新启动整个事情,但我更愿意找出我搞砸了并修复它。

非常感谢任何帮助。

马特

1 个答案:

答案 0 :(得分:9)

如果无法查看局部变量,则难以诊断问题。某个模型返回self.pageImages = apiResult["images"].array 而不是字符串,当它被要求为管理中的窗口小部件提供选项标签时,请选择我认为。

检查模型上的 str 方法。拥有FieldFile的人将成为罪魁祸首。