안녕하세요 저는 튜토리얼을 통해 장고 투표 앱을 만들었습니다. 나는 로그인 한 사용자가 선택에 투표 할 때 선택 섹션이 db에 저장되도록 추가하려고합니다. Models.pyDjango Polls Tutorial with Voterid with choice
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=400)
vote = models.IntegerField(default=0)
points = models.IntegerField(default=1)
def __str__(self):
return self.choice_text
class Voter(models.Model):
user = models.ForeignKey(User)
selections = models.CharField('question.choice', max_length=600)
내 Views.py 및 Vote.view :
class VoteView(generic.View):
def dispatch(self, request, *args, **kwargs):
# Getting current question
question = get_object_or_404(Question, pk=kwargs.get('question_id'))
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Display flash message
messages.error(request, "You didn't select a choice.")
# Redirect to the current question voting form again
return HttpResponseRedirect(reverse('questionaire:detail', args=(kwargs.get('question_id'),)))
else:
selected_choice.vote += 1
selected_choice.save()
v = Voter(user=request.user, Question=q)
v.save()
그래서 나중에 처리를 위해 저장 될 데이터베이스에 사용자 당 선택한 선택을 저장하려고하고 분석.
에
Voter
에서 모델 이름을 바꾸는 생각? –