1

我正在尝试保存我的模型表单。姜戈回归InternalError: current transaction is aborted, commands ignored until end of transaction block

这是执行Django 生成的INSERTsql-query时出现的问题。看起来像:

INSERT INTO "myapp_mymodel" ("title", ...) VALUES ("Test", ...) RETURNING "catalog_ad"."id"; args=("title", ...)

我试图在 PgAdmin 中执行这个查询。它返回了这个错误:

ERROR:  syntax error at or near "args"

有什么问题?

UPD:这是 InternalError 的回溯:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/<Path to my app>/views.py" in master
  115.             ad = form.save()
File "/Library/Python/2.7/site-packages/django/forms/models.py" in save
  364.                              fail_message, commit, construct=False)
File "/Library/Python/2.7/site-packages/django/forms/models.py" in save_instance
  86.         instance.save()
File "<Path to my app>/models.py" in save
  145.         super(Ad, self).save(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save
  463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save_base
  551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/Library/Python/2.7/site-packages/django/db/models/manager.py" in _insert
  203.         return insert_query(self.model, objs, fields, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in insert_query
  1576.     return query.get_compiler(using=using).execute_sql(return_id)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  910.             cursor.execute(sql, params)

UPD2:

我的模型覆盖了保存方法:

def save(self,  *args, **kwargs):
    self.title = self.model.brand.name + " " + self.model.name
    super(Ad, self).save(*args, **kwargs)

数据由使用的视图ModelForm处理(从 POST 获取数据)。保存前已验证表单 (form.is_valid())。

4

1 回答 1

1

我想说你的代码中有一个错误,它通过 Django 生成 INSERT 语句。如果没有看到该代码,很难说出问题所在。您发布的 SQL 中的分号结束了看起来有效的 SQL 语句。

args=("title", ...)部分在我看来不是 SQL。检查您的代码以查看可能意外地将其附加到生成 SQL 的任何内容。

于 2012-08-03T15:19:43.247 回答