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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With