django事务回滚-django-reversion /django-simple-history

django-simple-history

django-simple-history在每个创建/更新/删除操作中存储Django模型状态。

此应用程序支持Django和Python的以下组合:

Django的 蟒蛇
2.2 3.5、3.6、3.7、3.8、3.9
3.0 3.6、3.7、3.8、3.9

安装

从安装的PyPIpip

  1. $ pip install django-simple-history

配置

设定值

添加simple_history到您的INSTALLED_APPS

  1. INSTALLED_APPS = [
  2. # ...
  3. 'simple_history',
  4. ]

历史模型可以跟踪谁进行了每个更改。要自动填充历史记录用户,您可以将其添加HistoryRequestMiddleware到Django设置中:

  1. MIDDLEWARE = [
  2. # ...
  3. 'simple_history.middleware.HistoryRequestMiddleware',
  4. ]

如果您不想使用中间件,则可以按照User Tracking中的说明明确指示进行更改的用户

追踪历史

要跟踪模型的历史记录,请在模型上创建一个实例 simple_history.models.HistoricalRecords

跟踪Django教程中PollChoice模型上的更改的示例:

  1. from django.db import models
  2. from simple_history.models import HistoricalRecords
  3. class Poll(models.Model):
  4. question = models.CharField(max_length=200)
  5. pub_date = models.DateTimeField('date published')
  6. history = HistoricalRecords()
  7. class Choice(models.Model):
  8. poll = models.ForeignKey(Poll)
  9. choice_text = models.CharField(max_length=200)
  10. votes = models.IntegerField(default=0)
  11. history = HistoricalRecords()

现在,所有的变化PollChoice模型实例会在数据库中进行跟踪。

第三方模型的跟踪历史

要跟踪未创建模型的历史记录,请使用 simple_history.register函数。您可以使用它来跟踪不受您控制的第三方应用程序的模型。这是一个使用simple_history.register历史记录来跟踪应用程序中的User模型 的示例 django.contrib.auth

  1. from simple_history import register
  2. from django.contrib.auth.models import User
  3. register(User)

如果要将历史模型的迁移分离到第三方模型应用程序以外的应用程序中,可以在中设置app参数 register。例如,如果您希望迁移保存在您要在其中注册模型的软件包的迁移文件夹中,则可以执行以下操作:

  1. register(User, app=__package__)

运行迁移

进行模型更改后,创建并应用数据库迁移:

  1. $ python manage.py makemigrations
  2. $ python manage.py migrate

现有项目

对于现有项目,您可以调用populate命令为预先存在的模型实例生成初始更改:

  1. $ python manage.py populate_history --auto

默认情况下,历史记录行将以200为批次插入。如果需要使用大型表,可以使用--batchsize选项更改此行。--batchsize 500

现在怎么办?

通过添加HistoricalRecords到模型或使用来注册模型register,您将自动开始跟踪该模型上发生的所有创建,更新或删除。现在,您可以以编程方式查询历史记录,在Django admin中查看历史记录

什么是django-simple-history做幕后?

如果您尝试了上面的代码并对其进行了迁移,则会在数据库中看到以下表格:

  • app_choice
  • app_historicalchoice
  • app_historicalpoll
  • app_poll

historical名称前面的两个额外表是由创建的表django-simple-history。这些表存储您对它们各自的基本表所做的每个更改。每次在该模型的历史表中发生创建,更新或删除操作,ChoicePoll在该表的历史记录中创建新行时,包括基本模型实例中的所有字段以及其他元数据:

  • history_user:进行创建/更新/删除的用户
  • history_date:创建/更新/删除发生的日期时间
  • history_change_reason:创建/更新/删除发生的原因(默认为null)
  • history_id:历史表的主键(请注意,基本表的主键在历史表上不是唯一的,因为历史表上有多个版本)
  • history_type+用于创建,~更新和-删除

现在尝试保存Choice或的实例Poll。检查历史记录表以查看历史记录。

运行结果

C12EB8C3-0827-4a42-9E6A-D9ACD14E1AB8.png

django-reversion

管理员整合

django-reversion可用于向您的管理站点添加回滚和恢复。

警告

管理员集成要求您的数据库引擎支持事务。PostgreSQL,SQLite和MySQL InnoDB就是这种情况。如果使用MySQL MyISAM,请将数据库表升级到InnoDB!

总览

注册模型

使用reversion.admin.VersionAdmin的子类注册模型。

  1. from django.contrib import admin
  2. from reversion.admin import VersionAdmin
  3. @admin.register(YourModel)
  4. class YourModelAdmin(VersionAdmin):
  5. pass

暗示

每当您使用django-reversion注册模型时,请运行createinitialrevisions

注意

如果您已经使用reversion.register()注册了模型,则admin类将使用您在此处指定的配置。否则,管理类将遵循所有内联模型关系和父超类自动注册您的模型。通过覆盖VersionAdmin.register()来定制管理员注册。

与第三方应用程序集成

您可以将reversion.admin.VersionAdmin用作与第三方管理类的混合。

  1. @admin.register(SomeModel)
  2. class YourModelAdmin(VersionAdmin, SomeModelAdmin):
  3. pass

如果第三方模型已经向Django管理员注册,则可能必须先取消注册。

  1. admin.site.unregister(SomeModel)
  2. @admin.register(SomeModel)
  3. class YourModelAdmin(VersionAdmin, SomeModelAdmin):
  4. pass

运行结果

880A94AE-0744-48ca-ACB5-30DE99847903.png

总结