#35737 closed Cleanup/optimization (fixed)
Clarify where `models` comes from to understand missing import statement in tutorial 7
Reported by: | Mariatta | Owned by: | Mariatta |
---|---|---|---|
Component: | Documentation | Version: | 5.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
On this tutorial: https://docs.djangoproject.com/en/5.1/intro/tutorial07/#id8
It is missing the import for models
which is used to define the Question
model.
from django.db import models
Not adding the import statement could be confusing to the new learners, because throughout the tutorials, it uses models
from both django.db.models and polls.models
. On the previous examples of this tutorial page, it uses models
imported from the polls
app, so it would be useful to remind new learners that we're extending the models.Model
from django.db
and not the one from the polls.models
.
The same issue seems to appear in v 4 as well. I didn't check the earlier versions.
If you'd agree this is worth fixing, I can open a PR.
Change History (7)
comment:1 by , 4 months ago
Summary: | Missing import statement in tutorial → Clarify where `models` comes from to understand missing import statement in tutorial 7 |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Bug → Cleanup/optimization |
comment:2 by , 4 months ago
Sounds good, yes I think adding a link back to Tutorial 2 where it was created initially will be useful.
I can create a patch.
comment:3 by , 4 months ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:5 by , 4 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
Hello Mariatta! Thank you for taking the time to create this ticket.
I absolutely see how the example you provided could be confusing to some readers. On the other hand, the tutorial tries to prioritize clarity and focus on each tutorial part, and on each code snippet. This has a consequence that each code snippet (usually) builds on top of a previous one, but is not 100% complete in terms of imports.
The Tutorial parts are designed in a way that if the reader builds their code from scratch part after part, they end up with complete and correct code. The con is that every code snippet can not possible list all needed imports because the size of the snippet would grow considerably, and we risk confusing the user when it comes to understanding "what's new, what changed" from previous versions.
Having said this, I think we could improve this specific section by saying something similar to what other parts already include:
"You can improve that by using the display() decorator on that method (extending the existing polls/models.py that was created in Tutorial 2), as follows:"
(For example, Tutorial 5 says "In Tutorial 4 we introduced a class-based view, based on ListView: <incomplete code snippet follows>".)
The same strategy could be applied in other parts of the tutorial. I'll accept the ticket on that basis. Would you like to prepare a patch?