'DJANGO How to update model value +1 in database JSON on a click of a button

Clicking on update i get a Rest Api Page instead of Updating results in database. Thank You ALL

Views.py

@admin_only
@api_view(['GET','POST'])
def subs(request, pk):
    sw = get_object_or_404(Swimmers,id=pk).sessions
    sw_list =  sw
    sw_lists = sw + 1

    serializer = SubSerializer(instance=sw, data=request.data)

    if serializer.is_valid():
        serializer.save()

    return Response(serializer.data)

Urls.py

path('vswimmer/<int:pk>', views.VWmore.as_view(), name='view_swimmer'),
path('deletes/<int:pk>', views.SWGDelete.as_view(), name='deletes_sessions'),
path('subt/<int:pk>', views.subs, name='subt'),


Solution 1:[1]

You seem to be saving a SubSerializer instance using sw, but you never add to sw. You create a new object sw_lists and add 1 to it. The purpose of sw_list is unclear.

I think you want something more along the lines of

sw = get_object_or_404(Swimmers, id=pk)
current_sessions = sw.sessions
sw.sessions = current_sessions + 1
serializer = SubSerializer(instance=sw, data=request.data)
...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AMG