Vaibhav Jain
Vaibhav Jain

Reputation: 5507

Keep track of Django model History

I have Two models Packet and Item. Item has many2one relationship with Packet Model. I want to keep track of Both Models like. created/updated/deleted. Deleted items for a Packet model instance. What will be the best approach to solve this problem. Any third party apps ???? I tried creating exact replica of the deleted instances of Item Model using Django signals but is it the right way???

Models:

Packet 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
    )

Item Model:

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
    )

For ItemHistory:

    @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()

Upvotes: 0

Views: 3800

Answers (2)

Vaibhav Jain
Vaibhav Jain

Reputation: 5507

Found a nice app. Which provides awesome API and helpful in most of the cases. Like: It provides API to track Insert/Updated/Deleted instances with timestamp and User who did it. Documentation is written quite nicely too.

Attach:

audit_log = AuditLog() to the existing model and it will attach these extra fields to keep track of the changes in the model.

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.

Upvotes: 1

sirFunkenstine
sirFunkenstine

Reputation: 8495

Django-Reversion might be just what your looking for. After installing the app, you tag whatever models you want to add to reversion and then every time you save your model a copy gets saves in reversion. Then you can query your model reversion list using various parameters. its quite cool.

Upvotes: 2

Related Questions