How to use postgresql stored_procedure on Django Rest_framework

Viewed 40

I'm learning how to use django rest API for my project. I created a stored_procedure on my postgresql and I want to incorporate it to my project

I did make one but without serializers or any related to the rest API, just plain django

here's my views.py without the rest_framework

def add(request):
if request.method == "POST":
    if request.POST.get('office_id') and request.POST.get('sem') and request.POST.get('sy') and request.POST.get('incident_details') and request.POST.get('resolution') and request.POST.get('studid'):
        saverec = Clearanceinsert()
        saverec.office_id = request.POST.get('office_id')
        saverec.sem = request.POST.get('sem')
        saverec.sy = request.POST.get('sy')
        saverec.remarks = request.POST.get('incident_details')
        saverec.resolution = request.POST.get('resolution')
        saverec.studid = request.POST.get('studid')
        cursor = connection.cursor()
        email = request.user.userid
        cursor.execute("select add_clearance_item('"+saverec.office_id+"','"+saverec.sem+"','"+saverec.sy+"','"+saverec.remarks+"','"+saverec.resolution+"','"+email+"','"+saverec.studid+"')")
        return render(request, 'clearance/index.html')
else:
    return render(request, 'clearance/add.html')

views.py using rest_framework

@api_view(['POST'])
def saveitem(request):
if request.method == 'POST':
    saveserialize = Insertserialize(data=request.data)
    if saveserialize.is_valid():
        cursor = connection.cursor()
        cursor.execute("select add_clearance_item('"+saveserialize.office_id+"','"+saveserialize.sem+"','"+saveserialize.sy+"','"+saveserialize.remarks+"','"+saveserialize.resolution+"','"+saveserialize.studid+"')")
        return Response(saveserialize.data, status=status.HTTP_201_CREATED)
        return Response(saveserialize.data, status=status.HTTP_400_BAD_REQUEST)

When using rest_framework code, It's throwing an error 'Insertserialize' object has no attribute 'office_id'

serializer.py

class Insertserialize(serializers.ModelSerializer):
class Meta:
    model = ClearanceItem
    fields = [
        'office_id',
        'sem',
        'sy',
        'remarks',
        'resolution',
        'studid'
    ]

hope someone can help me with this

0 Answers
Related