Python Forum

Full Version: Update records in table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I tried to fund a solution to this problem : I havte tables and would increment counter (reservedplaces) in my application:
Here is the code.

The probelm is that any post request create a new entry in table instead of updating existing one

view :

class ReservationCancelView(mixins.RetrieveModelMixin,

                 mixins.ListModelMixin,

                 mixins.DestroyModelMixin,

                 mixins.CreateModelMixin,

                 mixins.UpdateModelMixin,

                 viewsets.GenericViewSet):

    #Model = Reservation

    serializer_class = ReservationCSerializer

    permission_classes = (permissions.IsAuthenticated,)    




    def get_queryset(self):

        resid = self.request.POST.get('id')  

        Res= Reservation.objects.get(id=resid)

        #Res.reservedplaces =F('reservedplaces ') - 1

        #Res.save()

        return Res




    def post(self, request, token=None, format=None):

        resid = self.request.POST.get('id')  

        travel_id = self.request.POST.get('travel_id')  
        Res= Reservation.objects.get(id=resid)
        Res.reservedplaces =F('reservedplaces ') - 1
        Res.travel_id = travel_id
        Res.save()



#serializer 

class ReservationCSerializer(serializers.ModelSerializer):

    user = Serializers.PrimaryKeyRelatedField(read_only=True,default=serializers.CurrentUserDefault())

    class Meta:
        model = Reservation
        fields = ('__all__')
urls.py

router.register(r'cancelreservation', ReservationCancelView,'reservationcancel')
i may or may not be able to help, questions...
may i know what is F('reservedplaces ') in line 43 or how was it derived? try print out Res.reservedplaces each time post requested, does it change ?