How to develop a rest api without using serializer in Django Rest Framework?

I want to create a Student Register and Login Api without using serializer in django Rest Framework. So I want to know how I make CRUD operation for those api using ApiView

Any one please solve this

Using serializers in Django Rest Framework (DRF) is a recommended approach for creating APIs because it provides a way to validate and convert the incoming request data into a format that is suitable for your Django models. Serializers also handle the encoding and decoding of the data for the API responses.

However, if you want to create a student register and login API without using serializers in DRF, you can do so by using the APIView class and handling the request data and responses manually.

Here is an example of how you can create a student register and login API without using serializers in DRF:

from django.contrib.auth import authenticate, login
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from django.views.generic import View

@csrf_exempt
@require_http_methods(["POST"])
def register(request):
    data = request.POST
    name = data.get('name')
    email = data.get('email')
    password = data.get('password')

    # Validate the data and check if the email is already registered
    if name and email and password:
        # Create a new student instance using the validated data
        student = Student.objects.create_user(name=name, email=email, password=password)
        student.save()
        return JsonResponse({'message': 'Success'})
    return JsonResponse({'error': 'Invalid data'})

@csrf_exempt
@require_http_methods(["POST"])
def login(request):
    data = request.POST
    email = data.get('email')
    password = data.get('password')

    # Validate the data and check if the email and password match a student in the database
    if email and password:
        student = authenticate(request, email=email, password=password)
        if student:
            login(request, student)
            return JsonResponse({'message': 'Success'})
        return JsonResponse({'error': 'Invalid email or password'})
    return JsonResponse({'error': 'Invalid data'})

In this example, I have defined two views: register and login. The register view handles the student registration by creating a new student instance using the validated data and saving it to the database. The login view handles the student login by authenticating the email and password and logging in the student if the email and password match a student in the database.

I don't think you can, or even should but first, you need to understand what a serializer actually does:

Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML, or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data

So don't be scared by serializers. Just take your time and learn how to use them using the many tutorials available online.

If you are using DRF then you must need to create the serializers. But you can create the API without using DRF and serialzers.

serializer is not just for converting json data ,it also used to validate payload.you can use request.data.get("field_name_in_json_object") and fill the fields one by one.and for return you can use .values("field_name") in querysets. Django values_list vs values

Back to Top