Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin: TypeError: __str__ returned non-string (type FieldFile)

Tags:

python

django

I have a functioning Django site and a functioning admin, except for when I try to use the admin site to make a new object from the model "Article." When I try to create a new "Article" I get this error:

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

Here's the weird thing: It was working this morning. I tried installing django-markdown and I started getting this error. I couldn't get it to work, so I uninstalled django-markdown AND reverted all the code back to the original (previously functional) and did the appropriate migrations.

But I'm still getting the error.

Here's the traceback and render error:

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 

Here's my Article model:

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)

I almost feel like uninstalling and then just re-starting the whole thing with the current setup, but I'd prefer to find out what I screwed up and just fix it.

Any help is greatly appreciated.

Matt

like image 974
Matt Payne Avatar asked Jan 09 '23 03:01

Matt Payne


1 Answers

Without being able to see the local variables it's difficult to diagnose the problem. A model somewhere is returning a FieldFile instead of a string when it's called for to provide option labels for a widget in the admin, select I think.

Check your str methods on your models. One that has a FileField would be the culprit.

like image 90
miyamoto Avatar answered Jan 10 '23 17:01

miyamoto