Django Admin:__str__在添加时返回非字符串(类型方法)

时间:2016-01-22 00:44:59

标签: python django admin pycharm

我在管理页面上尝试使用“添加”按钮创建新的“研究”实例时出现 str 错误。我可以在shell中创建一个模型实例没问题,但是Admin页面给了我一些问题。我不确定为什么它是非字符串错误,因为 str 正在调用Charfield。

from django.db import models
from django.conf import settings
from account.models import User, UserProfile
# Create your models here.


class StudySite(models.Model):
    name = models.CharField(max_length=200, null=True)
    address = models.CharField(max_length=200, null=True)
    city = models.CharField(max_length=100, null=True)
    state = models.CharField(max_length=2, null=True)

    def __str__(self):
        return self.name


class Study(models.Model):
    site = models.ForeignKey(StudySite, blank=True, null=True)
    title = models.TextField
    shortTitle = models.CharField(max_length=100, null=True)
    protocol = models.CharField(max_length=200, blank=True, null=True)
    PI = models.ForeignKey('account.UserProfile', limit_choices_to=       {'job_title': 'PI'}, related_name='study_PI',
                           blank=True, null=True)

    def __str__(self):
        return self.shortTitle


class IRB(models.Model):
    name = models.CharField(max_length=100)
    url = models.URLField()

任何想法。我在所有搜索中都没有看到(类型方法)错误,所以我很感激一些帮助。谢谢!

Here is the Traceback

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/studies/study/add/

Django Version: 1.8.7
Python Version: 3.5.0
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'studies',
 'account')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Template error:
In template /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19
   __str__ returned non-string (type method)

   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:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
  164.                 response = response.render()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/response.py" in render
  158.             self.content = self.rendered_content
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/response.py" in rendered_content
  135.         content = template.render(context, self._request)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/backends/django.py" in render
  74.         return self.template.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
  210.                     return self._render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in _render
  202.         return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/loader_tags.py" in render
  135.         return compiled_parent._render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in _render
  202.         return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/loader_tags.py" in render
  135.         return compiled_parent._render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in _render
  202.         return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/loader_tags.py" in render
  65.                 result = block.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/defaulttags.py" in render
  217.                             nodelist.append(node.render(context))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/loader_tags.py" in render
  159.                 return template.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
  212.                 return self._render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in _render
  202.         return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/defaulttags.py" in render
  217.                             nodelist.append(node.render(context))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/defaulttags.py" in render
  217.                             nodelist.append(node.render(context))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/defaulttags.py" in render
  329.                 return nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/defaulttags.py" in render
  329.                 return nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/template/debug.py" in render
  92.             output = force_text(output)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/encoding.py" in force_text
  90.                     s = six.text_type(s)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/html.py" in <lambda>
  399.         klass.__str__ = lambda self: mark_safe(klass_str(self))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/forms.py" in __str__
  537.         return self.as_widget()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/forms.py" in as_widget
  593.         return force_text(widget.render(name, self.value(), attrs=attrs))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/widgets.py" in render
  292.             'widget': self.widget.render(name, value, *args, **kwargs),
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/widgets.py" in render
  513.         options = self.render_options(choices, [value])
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/widgets.py" in render_options
  539.         for option_value, option_label in chain(self.choices, choices):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/models.py" in __iter__
  1108.                 yield self.choice(obj)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/models.py" in choice
  1115.         return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/models.py" in label_from_instance
  1186.         return smart_text(obj)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/encoding.py" in smart_text
  56.     return force_text(s, encoding, strings_only, errors)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/encoding.py" in force_text
  90.                     s = six.text_type(s)

Exception Type: TypeError at /admin/studies/study/add/
Exception Value: __str__ returned non-string (type method)

0 个答案:

没有答案