Update records in table - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Update records in table (/thread-8115.html) |
Update records in table - liee - Feb-06-2018 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') RE: Update records in table - ka06059 - Feb-07-2018 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 ? |