When saving a django model using it's save method is there any way to make sure nothing happened during save and send a message to the user? I was thinking of the message framework and try except block?
当使用它的save方法保存django模型时,是否有办法确保保存过程中没有发生任何事情并向用户发送消息?我在考虑消息框架,并尝试除了block?
try:
model.save()
add success message to message framework
except DatabaseError:
add error message to message framework
except TransactionManagementError:
add error message
Is this the right way to do that?Also which exception is more likely to be raised when trying to save a new instance of a model?I fairly new to django so be kind :)
这是正确的方法吗?此外,在尝试保存模型的新实例时,哪个异常更可能被引发?我对django很陌生,所以要友善一些:)
2 个解决方案
#1
1
You would generally want to divide this to two problems:
你通常会把这个问题分成两个:
- Content problems, i.e. you have tried to save the same row in the db twice, triggering an error caused by a database constraint. This would raise a catchable
IntegrityError
. See: https://docs.djangoproject.com/en/dev/ref/exceptions/#database-exceptions (Django 1.6+ has some more errors). You should probably catch those and use something likemessages.error
to notify the user. - 内容问题,即您试图在db中保存同一行两次,从而触发由数据库约束引起的错误。这将产生一个明显的完整性错误。参见:https://docs.djangoproject.com/en/dev/ref/exceptions/#database-exception (Django 1.6+还有一些错误)。您可能应该捕获这些并使用一些类似于消息的东西。通知用户错误。
- The database is down or having some other critical problem. You should probably avoid catching the errors, and let django handle it for you, and show your own 500 page until the db is back online. See: https://docs.djangoproject.com/en/dev/howto/error-reporting/ and https://docs.djangoproject.com/en/dev/ref/urls/#handler500
- 数据库出现故障或有其他关键问题。您可能应该避免捕捉错误,并让django为您处理它,并显示您自己的500页,直到db恢复在线。见:https://docs.djangoproject.com/en/dev/howto/error-reporting/和https://docs.djangoproject.com/en/dev/ref/urls/ # handler500
#2
1
My approach is to use a base abstract model that all my models extend and in which I override the save method in order to catch exceptions and rollback transactions:
我的方法是使用我所有模型扩展的一个基本抽象模型,并在其中重写save方法,以便捕获异常和回滚事务:
class AbstractModel(models.Model):
class Meta:
abstract = True
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
try:
super(AbstractModel, self).save(force_insert, force_update, using, update_fields)
except IntegrityError as saveException:
logger.exception('Rolling back transaction due to an error while saving %s...' % self.__class__.__name__)
try:
transaction.rollback()
except Exception as rollbackException:
logger.exception('Unable to rollback!')
#1
1
You would generally want to divide this to two problems:
你通常会把这个问题分成两个:
- Content problems, i.e. you have tried to save the same row in the db twice, triggering an error caused by a database constraint. This would raise a catchable
IntegrityError
. See: https://docs.djangoproject.com/en/dev/ref/exceptions/#database-exceptions (Django 1.6+ has some more errors). You should probably catch those and use something likemessages.error
to notify the user. - 内容问题,即您试图在db中保存同一行两次,从而触发由数据库约束引起的错误。这将产生一个明显的完整性错误。参见:https://docs.djangoproject.com/en/dev/ref/exceptions/#database-exception (Django 1.6+还有一些错误)。您可能应该捕获这些并使用一些类似于消息的东西。通知用户错误。
- The database is down or having some other critical problem. You should probably avoid catching the errors, and let django handle it for you, and show your own 500 page until the db is back online. See: https://docs.djangoproject.com/en/dev/howto/error-reporting/ and https://docs.djangoproject.com/en/dev/ref/urls/#handler500
- 数据库出现故障或有其他关键问题。您可能应该避免捕捉错误,并让django为您处理它,并显示您自己的500页,直到db恢复在线。见:https://docs.djangoproject.com/en/dev/howto/error-reporting/和https://docs.djangoproject.com/en/dev/ref/urls/ # handler500
#2
1
My approach is to use a base abstract model that all my models extend and in which I override the save method in order to catch exceptions and rollback transactions:
我的方法是使用我所有模型扩展的一个基本抽象模型,并在其中重写save方法,以便捕获异常和回滚事务:
class AbstractModel(models.Model):
class Meta:
abstract = True
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
try:
super(AbstractModel, self).save(force_insert, force_update, using, update_fields)
except IntegrityError as saveException:
logger.exception('Rolling back transaction due to an error while saving %s...' % self.__class__.__name__)
try:
transaction.rollback()
except Exception as rollbackException:
logger.exception('Unable to rollback!')