Click here to Skip to main content
15,434,829 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello my fellow brothers and sisters, i have been trying to take an integer value in the database and add/remove +/- 1, whenever i click on a button, its been killing me heating my head back and forth would appreciate the help. Thank YOU.

MYVIEWS.PY
<br />
def subs(request, pk):<br />
	sw = Swimmers.objects.filter(id=pk).values('sessions')<br />
	sw_list = map(int,sw)<br />
	sw_list = map(lambda x: x + 1, sw_list)<br />
	return JsonResponse(sw_list, safe=False)</pre><br />



MYMODELS.PY
<br />
<pre>class Swimmers(models.Model):<br />
	name = models.CharField(max_length=200, blank=False)<br />
	lastname = models.CharField(max_length=200, blank=False)<br />
	idno = models.CharField(max_length=200, blank=False, null=True)<br />
	sessions = models.IntegerField(blank=False)<br />
	totalsessions = models.CharField(max_length=200, blank=False )<br />
	dateofpayment = models.CharField(max_length=200, blank=False)<br />
	session = models.ForeignKey(Session, on_delete=models.SET_NULL, null=True)<br />
<br />
	def __str__(self):<br />
		return self.name</pre><br />


What I have tried:

i have tried making calculations on the model side but i works automatically rather than when i click on a button, so i tried to move my code to the VIEWS.PY
Posted
Updated 8-Apr-22 23:08pm

1 solution

The map() function[^] returns an iterator and you must convert it to a list before you can serialize it.
Python
return JsonResponse(list(sw_list), safe=False)
 
Share this answer
 
Comments
crevs ftb 9-Apr-22 6:10am     CRLF
Thank YOU For The Reply, i added list but encountered an error: int() argument must be a string, a bytes-like object or a number, not 'dict'
crevs ftb 9-Apr-22 6:12am     CRLF
def subs(request, pk): sw = Swimmers.objects.filter(id=pk).values('sessions') sw_list = map(int, sw) sw_list = map(lambda x: x + 1, sw_list) return JsonResponse(list(sw_list), safe=False)
crevs ftb 9-Apr-22 10:40am     CRLF
Hello Thank YOU Again, I managed to sort out the error by adding an index to int, but i am faced with a new error: invalid literal for int() with base 10: 'sessions' MYNEWCODE def subs(request, pk): sw = Swimmers.objects.filter(id=pk).values('sessions') sw_list = map(int, sw [0]) sw_list = map(lambda x: x + 1, sw_list ) return JsonResponse(list(sw_list), safe=False)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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