How can we keep records made in Django in the Log Table?
You can use the example below to keep the records made in Django in the log table.
from django.core import serializersfrom django.db import modelsfrom django.db.models.signals import post_save, pre_deletefrom main.middleware import get_current_userclass Loglar(models.Model): user = models.ForeignKey(User, verbose_name="User", on_delete= models.CASCADE, blank=True, null=True) model = models.CharField(max_length=30, default="", blank=True, null=True) modeld = models.CharField(max_length=250, default="", blank=True, null=True) status = models.CharField(max_length=30, default="", blank=True, null=True) note = models.TextField(default="", blank=True, null=True) create_date = models.DateTimeField("Save Time", auto_now_add=True)class Meta: verbose_name = "Transaction"verbose_name_plural = "Transaction Records"def onPostSave(sender, **kwargs):try:if not kwargs['instance']. _meta.object_name in ['LogEntry', 'Logs']: ## If only our tables are included in the log operations
model_instance = sender.objects.get(id=kwargs['instance'].id) raw_data = serializers.serialize('python', [model_instance]) processing_round = 'New Record' if kwargs['created'] else 'Update Record 'user = User.objects.get(username=get_current_user()) model = modeld = note = ""for d in raw_data: model = d['model'] modeld = d['pk'] note = dict(d[ 'fields']) log_kayit = Logs(user=user, status=operation_tour, model=model, modeld=modelid, note=note) log_kayit.save()except:passpost_save.connect(onPostSave)
You can click for our Türkiye location Web Hosting packages.
Can't find the information you're looking for?
You have examined the knowledge base in detail, but if you cannot find the information you need,
Create a Support Ticket-
How to Open Ports in Windows?
313 2
-
How to Grant Windows Administrator Privileges?
229 0
-
How to install Webmin on Ubuntu?
882 2
-
Linux SSH Terminal Commands and Explanations
797 3
-
CentOS Stream 10 Update Commands
856 1
-
CentOS Stream 9 Update Commands
1248 2
-
CentOS Stream 8 Update Commands
675 0
-
What is FreeBSD?
857 2
-
Out: 452 4.3.1 Insufficient system storage
768 2
-
Turning Off Automatic Updates in Windows Operating Systems
879 0