Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable logging of django rest api CRUD operations in django_admin_log?

I want to log all CRUD operations performed on Django Model Objects via REST framework implemented in django rest framework. I extend viewsets.ModelViewSet to create my custom viewSet class for defining REST API end points.

like image 508
attaboyabhipro Avatar asked Oct 17 '14 02:10

attaboyabhipro


People also ask

How do you do CRUD operations in Django REST framework?

First, we setup Django Project with a MySQL Client. Next, we create Rest Api app, add it with Django Rest Framework to the project. Next, we define data model and migrate it to the database. Then we write API Views and define Routes for handling all CRUD operations (including custom finder).

What is Restapi in Django?

Django REST framework (DRF) is a powerful and flexible toolkit for building Web APIs. Its main benefit is that it makes serialization much easier. Django REST framework is based on Django's class-based views, so it's an excellent option if you're familiar with Django.


1 Answers

There can be two different solutions...

1.Use signals in django to keep track of each operation in CRUD and make different model whose instance is created for each signal.Something like this....

signals.py 
@receiver(post_save, sender= Sender_model)
def crud_log(sender,created,**kwargs):
    obj= kwargs.get('instance')
    recipient=User.objects.get()
            Notification.objects.create(
                recipient= recipient,
                comment= obj,
                send_by=obj.supporter,
                text= "%s has commented on %s" % (obj.supporter,obj.project)
            )
            return None

here Notification is a model made by you to keep log of changes.

2.another solution is to use django-simple-history.

like image 67
Amrit Avatar answered Oct 12 '22 02:10

Amrit