跟踪 Django 模型历史
Keep track of Django model History
我有两个模型 Packet
和 Item
。 Item
有 many2one relationship
和 Packet
型号。我想跟踪两个模型。 created/updated/deleted
。为 Packet model
个实例删除了 items
。解决这个问题的最佳方法是什么。任何第三方应用程序????我尝试使用 Django signals
创建 Item Model
已删除实例的精确副本,但这是正确的方法吗???
型号:
数据包模型:
@python_2_unicode_compatible
class Packet(models.Model):
customer = models.ForeignKey(
Customer, verbose_name='related customer',
blank=False, null=False
)
created_on = models.DateField(
verbose_name='packet created on'
)
updated_on = models.DateTimeField(
verbose_name='packet last modified', blank=True
)
remark = models.TextField(
max_length=150, blank=True, null=True
)
created_by = models.ForeignKey(
User, related_name='packet_created_by',
on_delete=models.SET_NULL, null=True, blank=False
)
updated_by = models.ForeignKey(
User, related_name='packet_updated_by',
on_delete=models.SET_NULL, null=True, blank=False
)
物品型号:
class Item(models.Model):
name = models.ForeignKey(
ItemMaster, on_delete=models.PROTECT
)
packet = models.ForeignKey(
Packet, verbose_name='related packet'
)
created_on = models.DateField(blank=True)
updated_on = models.DateField(blank=True)
remark = models.TextField(
max_length=150, blank=True
)
created_by = models.ForeignKey(
User, related_name='item_created_by', null=True, on_delete=models.SET_NULL
)
updated_by = models.ForeignKey(
User, related_name='item_updated_by', null=True, on_delete=models.SET_NULL
)
对于物品历史:
@receiver(pre_delete, sender='girvi.Item')
def copy_item_details(sender, instance, **kwargs):
i = ItemHistory(type=instance.type, name=instance.name, packet=instance.packet, created_on=instance.created_on,
remark=instance.remark, created_by=instance.created_by, deleted_on=timezone.now()
)
i.save()
找到一个不错的app。这提供了很棒的 API 并且在大多数情况下都有帮助。
喜欢:它提供 API 来跟踪 Insert/Updated/Deleted 具有时间戳和执行此操作的用户的实例。文档也写得很好。
附加:
audit_log = AuditLog() 到现有模型,它将附加这些额外字段以跟踪模型中的更改。
action_id - Primary key for the log entry.
action_date - The point in time when the logged action was performed.
action_user - The user that performed the logged action.
action_type - The type of the action (Created/Changed/Deleted)
Any field of the original X model that is tracked by the audit log.
Django-Reversion 可能正是您要寻找的。安装该应用程序后,您可以标记要添加到还原的任何模型,然后每次保存模型时都会在还原中保存一个副本。然后您可以使用各种参数查询您的模型还原列表。很酷。
我有两个模型 Packet
和 Item
。 Item
有 many2one relationship
和 Packet
型号。我想跟踪两个模型。 created/updated/deleted
。为 Packet model
个实例删除了 items
。解决这个问题的最佳方法是什么。任何第三方应用程序????我尝试使用 Django signals
创建 Item Model
已删除实例的精确副本,但这是正确的方法吗???
型号:
数据包模型:
@python_2_unicode_compatible
class Packet(models.Model):
customer = models.ForeignKey(
Customer, verbose_name='related customer',
blank=False, null=False
)
created_on = models.DateField(
verbose_name='packet created on'
)
updated_on = models.DateTimeField(
verbose_name='packet last modified', blank=True
)
remark = models.TextField(
max_length=150, blank=True, null=True
)
created_by = models.ForeignKey(
User, related_name='packet_created_by',
on_delete=models.SET_NULL, null=True, blank=False
)
updated_by = models.ForeignKey(
User, related_name='packet_updated_by',
on_delete=models.SET_NULL, null=True, blank=False
)
物品型号:
class Item(models.Model):
name = models.ForeignKey(
ItemMaster, on_delete=models.PROTECT
)
packet = models.ForeignKey(
Packet, verbose_name='related packet'
)
created_on = models.DateField(blank=True)
updated_on = models.DateField(blank=True)
remark = models.TextField(
max_length=150, blank=True
)
created_by = models.ForeignKey(
User, related_name='item_created_by', null=True, on_delete=models.SET_NULL
)
updated_by = models.ForeignKey(
User, related_name='item_updated_by', null=True, on_delete=models.SET_NULL
)
对于物品历史:
@receiver(pre_delete, sender='girvi.Item')
def copy_item_details(sender, instance, **kwargs):
i = ItemHistory(type=instance.type, name=instance.name, packet=instance.packet, created_on=instance.created_on,
remark=instance.remark, created_by=instance.created_by, deleted_on=timezone.now()
)
i.save()
找到一个不错的app。这提供了很棒的 API 并且在大多数情况下都有帮助。 喜欢:它提供 API 来跟踪 Insert/Updated/Deleted 具有时间戳和执行此操作的用户的实例。文档也写得很好。
附加:
audit_log = AuditLog() 到现有模型,它将附加这些额外字段以跟踪模型中的更改。
action_id - Primary key for the log entry.
action_date - The point in time when the logged action was performed.
action_user - The user that performed the logged action.
action_type - The type of the action (Created/Changed/Deleted)
Any field of the original X model that is tracked by the audit log.
Django-Reversion 可能正是您要寻找的。安装该应用程序后,您可以标记要添加到还原的任何模型,然后每次保存模型时都会在还原中保存一个副本。然后您可以使用各种参数查询您的模型还原列表。很酷。