diff options
| author | Carlton Gibson <carlton.gibson@noumenal.es> | 2024-02-20 16:26:21 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2024-02-21 08:15:19 +0100 |
| commit | 5d9be66c98fe4e16a0295d0252d1c944980a822e (patch) | |
| tree | dfb861d9c87d90ea88e4d36c4095241d535ca542 | |
| parent | 69e5b13c758cec55edbab342da8be509557589f4 (diff) | |
[5.0.x] Removed distracting note from tutorial 4.
The note on a possible race condition is inappropriate in this
tutorial setting. To quote Diátaxis:
> Your job is to guide the learner to a successful conclusion. There
> may be many interesting diversions along the way … - ignore them.
Co-Authored-By: Ryan Hiebert <ryan@ryanhiebert.com>
Backport of 0a646c8e08605ba6896ef8f2938231d23c2181cc from main
| -rw-r--r-- | docs/intro/tutorial04.txt | 19 |
1 files changed, 5 insertions, 14 deletions
diff --git a/docs/intro/tutorial04.txt b/docs/intro/tutorial04.txt index 65dc132a94..e6ec6895fe 100644 --- a/docs/intro/tutorial04.txt +++ b/docs/intro/tutorial04.txt @@ -74,6 +74,7 @@ create a real version. Add the following to ``polls/views.py``: .. code-block:: python :caption: ``polls/views.py`` + from django.db.models import F from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reverse @@ -97,7 +98,7 @@ create a real version. Add the following to ``polls/views.py``: }, ) else: - selected_choice.votes += 1 + selected_choice.votes = F("votes") + 1 selected_choice.save() # Always return an HttpResponseRedirect after successfully dealing # with POST data. This prevents data from being posted twice if a @@ -123,6 +124,9 @@ This code includes a few things we haven't covered yet in this tutorial: :exc:`KeyError` and redisplays the question form with an error message if ``choice`` isn't given. +* ``F("votes") + 1`` :ref:`instructs the database + <avoiding-race-conditions-using-f>` to increase the vote count by 1. + * After incrementing the choice count, the code returns an :class:`~django.http.HttpResponseRedirect` rather than a normal :class:`~django.http.HttpResponse`. @@ -190,19 +194,6 @@ Now, go to ``/polls/1/`` in your browser and vote in the question. You should se results page that gets updated each time you vote. If you submit the form without having chosen a choice, you should see the error message. -.. note:: - The code for our ``vote()`` view does have a small problem. It first gets - the ``selected_choice`` object from the database, then computes the new - value of ``votes``, and then saves it back to the database. If two users of - your website try to vote at *exactly the same time*, this might go wrong: - The same value, let's say 42, will be retrieved for ``votes``. Then, for - both users the new value of 43 is computed and saved, but 44 would be the - expected value. - - This is called a *race condition*. If you are interested, you can read - :ref:`avoiding-race-conditions-using-f` to learn how you can solve this - issue. - Use generic views: Less code is better ====================================== |
