Click here to Skip to main content
15,353,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
**I have been trying to update and add + 1 in the database model (sessions) field whenever a button is being clicked. i am trying to be able to add +1 in a database field whenever a user clicks on a button and have that +1 record and time when the button was clicked in a HTML table, Thank You All, Much Appreciate** 

***Views.py***

    @admin_only
    @api_view(['POST'])
    def swimmersUpdate(request, pk):
    	swimmers = Swimmers.objects.get(id=pk)
    	cs = swimmers.sessions
    	swimmers.sessions = cs + 1
    
    	serializer = SubSerializer(instance=cs, data=request.data)
    
    	if serializer.is_valid():
    		serializer.save()
    		return Response(serializer.data, status=status.HTTP_201_CREATED)
    		
    	return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)

***Serializers.py***

    class SubSerializer(serializers.ModelSerializer):
        class Meta:
            model = Swimmers
            fields ='__all__'

***Urls.py***

    path('apiurls/', views.apiurls, name='apiurls'),
    path('swimmers-list/', views.swimmersList, name='swimmers-list'),
    path('swimmers-details/<int:pk>', views.swimmersDetails, name='swimmers-details'),
    path('swimmers-create/', views.swimmersCreate, name='swimmers-create'),
    path('swimmers-update/<int:pk>', views.swimmersUpdate, name='swimmers-update'),
    path('swimmers-delete/<int:pk>', views.swimmersDelete, name='swimmers-delete'),

***Models.py***

    class Swimmers(models.Model):
    	name = models.CharField(max_length=200, blank=False)
    	lastname = models.CharField(max_length=200, blank=False)
    	idno = models.CharField(max_length=200, blank=False, null=True)
    	sessions = models.IntegerField(blank=False)
    	totalsessions = models.CharField(max_length=200, blank=True )
    	dateofpayment = models.CharField(max_length=200, blank=True)
    	phone = models.CharField(max_length=30, blank=False, null=True)
    	date_from = models.DateField(null=True)
    	date_to = models.DateField(null=True)
    	type_choice = (
    		("basic", "Basic"),
    		("3x1 week", "3x1 Week"),
    		("1 session", "1 Session"),
    		("2x1", "2x1"),
    	)
    	type = models.CharField(max_length=200, blank=False, null=True, 
    		choices=type_choice, default=type_choice)
    	ammount = models.DecimalField(max_digits=6, decimal_places=2, blank=False, null=True)
    	registration = models.DateField(default=timezone.now)
    	keenphone = models.CharField(max_length=30, blank=False, null=True)
    
    	def __str__(self):
    		return self.name


What I have tried:

I have tried normal methods but not am using Rest.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900