Feb-06-2018, 03:52 PM
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 :
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')