def vote(request, poll_id): p = get_object_or_404(Poll, pk=poll_id) try: selected_choice = p.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): # Redisplay the poll voting form. return render_to_response('polls/detail.html', { 'poll': p, 'error_message': "You didn't select a choice.", }, context_instance=RequestContext(request)) else: selected_choice.votes += 1 selected_choice.save() # Always return an HttpResponseRedirect after successfully dealing # with POST data. This prevents data from being posted twice if a # user hits the Back button. return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))
selected_choice = p.choice_set.get(pk=request.POST['choice'])
selected_choice.votes += 1 selected_choice.save()
Hey,
On Wed, 11 Jul 2012 08:36:39 -0700 (PDT), JJ Zolper
<codin...@gmail.com> declaimed the following in
gmane.comp.python.django.user:
> So originally:
>
> selected_choice = p.choice_set.get(pk=request.POST['choice'])
>
If I understand this (I've not run the tutorial, and only browsed
the now-outdated print books), this statement is using the value from
the "choice" field of the submitted form as the primary key to retrieve
a record (model instance) from the database.
pseudo-SQL
select * from choice_set where pk = "request.POST['choice']"
> this requests the submitted choice from the POST data and
>
> The part that says:
>
> selected_choice.votes += 1
> selected_choice.save()
>
> Actually saves the choice to the database?
>
This then increments the votes field of the record (model instance)
retrieved from the database, and then saves the record back.
p-SQL
update choice_set set
votes = votes + 1
where pk = selected_choice.pk
{I don't know if Django is smart enough to only update the changed
field, or if it updates the entire record}
--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com HTTP://wlfraed.home.netcom.com/
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
On Wed, 11 Jul 2012 08:36:39 -0700 (PDT), JJ Zolper
<codin...@gmail.com> declaimed the following in
gmane.comp.python.django.user:
> So originally:
>
> selected_choice = p.choice_set.get(pk=request.POST['choice'])
>
If I understand this (I've not run the tutorial, and only browsed
the now-outdated print books), this statement is using the value from
the "choice" field of the submitted form as the primary key to retrieve
a record (model instance) from the database.
pseudo-SQL
select * from choice_set where pk = "request.POST['choice']"
> this requests the submitted choice from the POST data and
>
> The part that says:
>
> selected_choice.votes += 1
> selected_choice.save()
>
> Actually saves the choice to the database?
>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/JTdViX4I6HEJ.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.