我有一个自定义保存方法和一个自定义装饰器,可以在我的自定义保存之前和之后运行 Django 的模型 save():
模型.py:
from django.contrib.auth.models import User
from django.db import models
def save_decorator(method_to_decorate):
def wrapper(self, *args, **kwargs):
super(type(self), self).save(*args, **kwargs)
method_to_decorate(self, *args, **kwargs)
super(type(self), self).save(*args, ** kwargs)
return wrapper
class The_Image_Abstract(models.Model):
class Meta:
abstract = True
create_time = models.DateTimeField(editable=False)
class Avatar(The_Image_Abstract):
#I'm using this to track Avatar class in the template. There should be a better way.
user = models.OneToOneField(User, related_name='avatar')
@save_decorator
def save(self, *args, **kwargs):
"my stuff here"
pass
当 Avatar 在管理页面中保存或修改时,这非常有效。但是,当 Avatar 在另一个模型的内联中保存为表单集时,它会引发内部错误(表单集在添加装饰器之前工作)。这里出了什么问题?我看到有人在使用 Postgres 时收到此错误的帖子,而且我也在使用 Postgres,但我不认为这种情况是由 Postgres 引起的。
Request Method: POST
Request URL: http://localhost/admin/auth/normal_user/add/
Django Version: 1.4.3
Exception Type: InternalError
Exception Value:
current transaction is aborted, commands ignored until end of transaction block
Exception Location: /home/eras/projects/kart/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py in execute_sql, line 912
Python Executable: /home/eras/projects/kart/venv/bin/python
Python Version: 2.7.3
任何帮助表示赞赏!
谢谢,
时代