1👍
Django admin uses generic foreign keys to handle your case so you should probably do something like that. Let’s take a look at how django admn does it (https://github.com/django/django/blob/master/django/contrib/admin/models.py):
class LogEntry(models.Model): action_time = models.DateTimeField(_('action time'), auto_now=True) user = models.ForeignKey(settings.AUTH_USER_MODEL) content_type = models.ForeignKey(ContentType, blank=True, null=True) object_id = models.TextField(_('object id'), blank=True, null=True) object_repr = models.CharField(_('object repr'), max_length=200) action_flag = models.PositiveSmallIntegerField(_('action flag')) change_message = models.TextField(_('change message'), blank=True)
So, you can add an additional model (LogEntry
) that will hold a ForeignKey
to the user that changed (added / modified) the object and a GenericForeignKey
(https://docs.djangoproject.com/en/1.7/ref/contrib/contenttypes/#generic-relations) to the object that was modified.
Then, you can modify your views to add LogEntry
objects when objects are modified. When you want to display all changes by a User
, just do something like:
user = User.objects.get(pk=1) changes = LogEntry.objects.filter(user=user) # Now you can use changes for your requirement!
I’ve written a nice blog post about that (auditing objects in django) which could be useful: http://spapas.github.io/2015/01/21/django-model-auditing/#adding-simple-auditing-functionality-ourselves