使用Django(一般来说是新手,也是Python的新手)来做一个非常基本的联系人数据库。
尝试通过管理页面将记录添加到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__'
感谢任何帮助。挣扎!感谢。
马特
答案 0 :(得分:14)
您使用__unicode__
方法打印,而不是返回字符串。
答案 1 :(得分: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>
希望现在更清楚了。