4

使用 Django(它是新手,通常是 Python 新手)来做一个非常基本的联系人数据库。

尝试通过 Admin 页面将记录添加到 Contributor 表,其模型代码为:

class Contributor(models.Model):

    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    organisation = models.ForeignKey('Organisation')
    mode = models.CharField(max_length=10, blank=True)
    email = models.CharField(max_length=30, blank=True)
    landline = models.CharField(max_length=10, blank=True)

def __unicode__(self):
    print('%s %s' % (self.first_name, self.last_name))

我收到的错误消息是:

    Environment:


    Request Method: POST
    Request URL: http://127.0.0.1:8000/admin/ramapp/contributor/add/

    Django Version: 1.5.5
    Python Version: 2.7.5
    Installed Applications:
    ('django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.sites',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'django.contrib.admin',
     'south',
     'ramapp')
    Installed Middleware:
    ('django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware')


    Traceback:
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
      115.                         response = callback(request, *callback_args, **callback_kwargs)
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
      372.                 return self.admin_site.admin_view(view)(*args, **kwargs)
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
      91.                     response = view_func(request, *args, **kwargs)
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
      89.         response = view_func(request, *args, **kwargs)
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
      202.             return view(request, *args, **kwargs)
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
      25.             return bound_func(*args, **kwargs)
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
      91.                     response = view_func(request, *args, **kwargs)
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
      21.                 return func(self, *args2, **kwargs2)
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/db/transaction.py" in inner
      223.                 return func(*args, **kwargs)
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view
      1009.                 self.log_addition(request, new_object)
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-packages/django/contrib/admin/options.py" in log_addition
      530.             action_flag     = ADDITION
    File "/Users/lemon/.virtualenvs/ram/lib/python2.7/site-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/ramapp/contributor/add/
    Exception Value: 'NoneType' object has no attribute '__getitem__'

感谢任何帮助。挣扎!谢谢。

马特

4

2 回答 2

15

您正在打印您的__unicode__方法,而不是返回字符串。

于 2013-11-06T16:10:29.977 回答
5

是的,您的解决方案已经对了吗?

class Contributor(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    organisation = models.ForeignKey('Organisation')
    mode = models.CharField(max_length=10, blank=True)
    email = models.CharField(max_length=30, blank=True)
    landline = models.CharField(max_length=10, blank=True)

    def __unicode__(self):
        return '%s %s' % (self.first_name, self.last_name)

顺便说一句,因为您是 Python 新手。函数总是返回一些东西,当没有return语句时,它返回None

>>> def a():
...     print 'Hello there!' # No return statement
... 
>>> def b():
...     return 'Bye!'
... 
>>> result_a = a()
Hello there!
>>> print result_a
None
>>> result_b = b()
>>> print result_b
Bye!
>>> 

当然,NoneType对象没有方法__getitem__,而str对象有:

>>> result_a.__getitem__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute '__getitem__'
>>> result_b.__getitem__
<method-wrapper '__getitem__' of str object at 0xb7278e00>

希望现在更清楚了。

于 2013-11-06T20:33:08.640 回答