Reputation: 57
How to i update a certain value in the database?
class Userdata(models.Model):
user = models.OneToOneField(User, on_delete= models.CASCADE)
faculty = models.ForeignKey(Fakultas,on_delete=models.CASCADE,default= 1)
is_voted = models.BooleanField(default=False)
def __str__(self):return self.user.username
class Voting(models.Model):
name = models.CharField(max_length=50)
faculty = models.ForeignKey(Fakultas, on_delete=models.CASCADE, default=1)
pic = models.CharField(max_length=50)
text = models.TextField()
voters = models.IntegerField()
def __str__(self): return self.name
My Views :
def voted(response):
if response.method == 'POST':
id = response.POST.get['idcalon']
user = Userdata.objects.get() #get the username
calon2 = Voting.objects.get() #get user selection in html
user.is_voted = True
calon2.voters +=1
user.save(['is_voted'])
calon2.save(['voters'])
I'm trying to grab the user's name and then when update the user's is_voted value to True when triggered.
Then, I wanted to grab my Voting model by id, for example, I wanted to edit id = 1
So how do I do it? I've been trying to understand the documentation, but still have 0 idea how to do it.
Thank you
Upvotes: 0
Views: 35
Reputation: 1412
def voted(request):
if request.method == 'POST':
id = request.POST.get['idcalon']
user = Userdata.objects.get(id=request.user.id) #get the username
calon2 = Voting.objects.get(id=id) #get user selection in html
user.is_voted = True
calon2.voters +=1
user.save()
calon2.save()
this will work if user is authenticated
Upvotes: 1