Python 对象没有属性“__getitem__”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16604915/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:09:24  来源:igfitidea点击:
object has no attribute '__getitem__'
提问by 2 8
I have two model like this:
我有两个这样的模型:
class School(models.Model):
    name = models.CharField(max_length = 50)
    def __unicode__(self):
        return self.name 
class Education(models.Model):
    user_profile = models.ForeignKey(UserProfile, related_name='Education')
    school = models.OneToOneField(School)
    def __unicode__(self):
        return self.school
When I want add a education to userprofile with django admin this error eccour:
当我想使用 django admin 向用户配置文件添加教育时,出现此错误:
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in wrapper
  372.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
  202.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.7/dist-packages/django/db/transaction.py" in inner
  223.                 return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in add_view
  1009.                 self.log_addition(request, new_object)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py" in log_addition
  530.             action_flag     = ADDITION
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/models.py" in log_action
  18.         e = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message)
Exception Type: TypeError at /admin/social/education/add/
Exception Value: 'School' object has no attribute '__getitem__'
How I can Fix this error ?
我怎样才能修复这个错误?
采纳答案by Alexey Kachayev
To fix this you need __unicode__to return str(not an object).
要解决此问题,您需要__unicode__返回str(不是对象)。
def __unicode__(self):
    return unicode(self.school)

