Django - Created_by udpated_by in all models

Hi all,

I came into a scenario where i wanted to have created_by in all tables even master and transaction as part of the audit. can I implement that in base model? as below kindly suggest some generic way

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone


class BaseModel(models.Model):
    created_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, editable=False)
    created_at = models.DateTimeField(default=timezone.now, editable=False)

    def save(self, *args, **kwargs):
        self.created_by = self.request.user
        super().save(*args, **kwargs)

    class Meta:
        abstract = True

Hi @vinothrajs
You can surely implement this in base model. I’d suggest using the Django CRUM package to get the curent user while saving your model, you can take idea from an example given in their official docs here: Django-CRUM — Django-CRUM 0.7.9 documentation