Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 5, 2022 08:32 am GMT

Automatically Add Logged In User Under 'created_by' and 'updated_by' to Model in Django Rest Framework

In some application, we need to track which user added or updated the data in the system. So here we will see how to do this job in the background without any user input.

Required setup:
I hope you already have Django and the Django Rest Framework installed. If not, please install these first.

run -

pip install Django pip install djangorestframework 

Settings:

In your settings.py add 'rest_framework' and your internal apps e.g. 'api'.

INSTALLED_APPS = [    ......    ......    ......    # third-party packages    'rest_framework',    # internal apps    'api',]

Write models:

Define your applications model in models.py

from django.db import modelsfrom django.conf import settingsclass Student(models.Model):    name = models.CharField(max_length=180)    programme = models.CharField(max_length=250)    batch = models.SmallIntegerField()    roll = models.SmallIntegerField()    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, related_name='created_by', blank=True, null=True)    updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, related_name='updated_by', blank=True, null=True)

Write Serializers:

Inside your application, add a file called serializers.py and define a serializer class for your model as follows:

from rest_framework import serializersfrom .models import Studentclass StudentModelSerializer(serializers.ModelSerializer):    created_by = serializers.StringRelatedField(default=serializers.CurrentUserDefault(), read_only=True)    updated_by = serializers.StringRelatedField(default=serializers.CurrentUserDefault(), read_only=True)    class Meta:        model = Student        fields = ['id', 'name', 'programme', 'batch', 'roll', 'created_by', 'updated_by']

Admin:

You can register your model in admin.py in case you want to add data from the Django admin panel:

from django.contrib import adminfrom .models import [email protected](Student)class StudentAdmin(admin.ModelAdmin):    list_display = ['id', 'name', 'programme', 'batch', 'roll']

View:

Now, write your view.py, and please make sure to add authentication as we have to work with users:

from .models import Studentfrom .serializers import StudentModelSerializerfrom rest_framework import viewsetsfrom rest_framework.permissions import IsAuthenticatedOrReadOnlyfrom rest_framework.authentication import SessionAuthenticationclass StudentModelViewSet(viewsets.ModelViewSet):    queryset = Student.objects.all()    serializer_class = StudentModelSerializer    authentication_classes = [SessionAuthentication]    permission_classes = [IsAuthenticatedOrReadOnly]    def perform_create(self, serializer):        serializer.save(created_by=self.request.user)    def perform_update(self, serializer):        serializer.save(updated_by=self.request.user)

URLs:

If you didn't define URLs for your views, then define it in urls.py.

from django.contrib import adminfrom django.urls import path, include from rest_framework.routers import DefaultRouterfrom api.views import *  # Create Router Objectrouter = DefaultRouter()# Register View Class with Routerrouter.register('student', StudentModelViewSet, basename='student_')urlpatterns = [    path('admin/', admin.site.urls),    path('api/', include(router.urls)),    path('auth/', include('rest_framework.urls', namespace='rest_framework'))]

Make Migrations:

Migration is necessary after changes in models.

run -

python manage.py makemigrations python manage.py migrate

Users:

So this is what we are working for; to add a logged-in user automatically, you must have users registered in your system. So if you don't have any registered users, then add some. If you are using the Django default system, then you can manage users from the Django admin panel.

It's time to see the outcomes.

Go to the URL in browser:

Go to the URL in browser

Login to a user:

Login to a user

Add a new data:

Add a new data

Look at the data, automatically added logged user info to created_by field:

Look at the data, automatically added logged user info to created_by field

Now update a data:

Now update a data

Look at the data, automatically added logged user info to updated_by field:

Look at the data, automatically added logged user info to updated_by field


Original Link: https://dev.to/forhadakhan/automatically-add-logged-in-user-under-createdby-and-updatedby-to-model-in-django-rest-framework-4c9c

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To