1

我的项目在 Django-1.2.5 和 Django-1.3.1 上运行良好,但在 Django-1.4.1 上运行良好。

我无法保存/创建对象并收到如下错误:

from entry.models import Entry

import datetime 

from publications.models import Publication

e = Entry(title=u'this is a test headline to test django-1.4.1',
          body_html=u'this is a test body data to test django-1.4.1',
          pub_date=datetime.datetime.now(),
          publication=Publication.objects.get(id=1))

e.save()

错误:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/admin/banking_code/contify-banking/entry/models.py", line 112, in save
  super(Entry, self).save()
File "/home/admin/banking_code/contify-banking/django/db/models/base.py", line 463, in save
  self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/admin/banking_code/contify-banking/django/db/models/base.py", line 551, in save_base
  result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/home/admin/banking_code/contify-banking/django/db/models/manager.py", line 203, in _insert
  return insert_query(self.model, objs, fields, **kwargs)
File "/home/admin/banking_code/contify-banking/django/db/models/query.py", line 1593, in insert_query
  return query.get_compiler(using=using).execute_sql(return_id)
File "/home/admin/banking_code/contify-banking/django/db/models/sql/compiler.py", line 914, in execute_sql
  return self.connection.ops.fetch_returned_insert_id(cursor)
File "/home/admin/banking_code/contify-banking/django/db/backends/__init__.py", line 548, in fetch_returned_insert_id
  return cursor.fetchone()[0]
TypeError: 'NoneType' object is not subscriptable

任何人都可以帮我解决这个问题吗?

4

1 回答 1

0

您能否验证是否存在 id = 1 的发布对象?最快的方法是通过 python shell:

从您的 django 项目的主文件夹中运行“python manage.py shell”,然后:

 >> from publications.models import Publication
 >> p = Publication.objects.get(id=1)
 >> p

如果这成功了(你得到一个 id 为 1 的 Publication 对象),那么我们将不得不考虑一个更奇特的失败原因。如果失败,问题将被缩小为发布模型定义的问题。

于 2012-10-09T09:33:44.770 回答