Как увеличить поле голосования в базе данных Django системы голосования - PullRequest
0 голосов
/ 18 февраля 2020

Я пытаюсь создать систему, в которой человек (избиратель) будет голосовать за кандидата против своего «идентификатора кандидата», поэтому для этого избиратель должен ввести «идентификатор эмирата» (который является основным ключом избирателя) и «Идентификатор выборов», который является первичным ключом избирательного участка - оба эти идентификатора будут проверены, чтобы проверить, подан ли голос или нет, но проблема действительно лежит в моей логике c. вот мой Views.py :

def vote(request):
        can_id_obj = Candidate_Form.objects.get(pk=request.POST['Can_ID'])
        voterrecord = Election_Day(  #through this we will create an object of the class or our model 
            Can = can_id_obj,   #these are the fields in my 'Election Day' model
            emirates_id = Emirates_ID,
            election_id = Election_ID
        )
        voterrecord.Vote += 1   #we will call a field to manipulate it
        voterrecord.save()  #finally we can call save method on the object
        return render(request, 'Election_Day.html')

Каждый раз, когда голосование опрашивается, вместо того, чтобы увеличивать ту же запись, за которую ранее был отдан голос, он будет делать новую запись, а затем сохранить голос как 1, так что в основном я хочу, чтобы он увеличивал ту же запись, если голосование было опрошено за того же кандидата. Надеюсь, вы понимаете объяснение ..

Models.py Для справки

class Election_Day(models.Model):           
    Can = models.ForeignKey('Candidate_Form', on_delete=models.CASCADE)  #you refer the primary key to the whole class and it will automatically pick the id (primary key) of that class and make it a foreign key      #with that you can also access all the data stored in that table
    Elec = models.ForeignKey('Election_Info', on_delete=models.CASCADE, null = 
    True) #you can make a foreign key null
    Vote = models.IntegerField(default=0)
    emirates_id = models.IntegerField()
    election_id = models.IntegerField()

1 Ответ

0 голосов
/ 18 февраля 2020

Вы не сохраняете Candidate_Form объект. Просто добавьте can_id_obj.save() в конце, как это.

def vote(request):
        can_id_obj = Candidate_Form.objects.get(pk=request.POST['Can_ID'])
        voterrecord = Election_Day(  #through this we will create an object of the class or our model 
            Can = can_id_obj,   #these are the fields in my 'Election Day' model
            emirates_id = Emirates_ID,
            election_id = Election_ID
        )
        can_id_obj.Vote += 1   #we will call a field to manipulate it
        can_id_obj.save()
        voterrecord.save()  #finally we can call save method on the object
        return render(request, 'Election_Day.html')
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...