#32741 closed Cleanup/optimization (worksforme)
Tutorial Part 2 throws models.W042 warnings when making migrations
Reported by: | Katie McLaughlin | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | 3.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
Changes in Django 2.2 mean that the auto-created primary key changes (https://docs.djangoproject.com/en/3.2/releases/3.2/#customizing-type-of-auto-created-primary-keys) throw the associated warnings, as the Tutorial Part 2 doesn't adjust for these.
Reproduction:
- in a new virtualenv,
pip install django==3.2.2
- generate a new template project
django-admin startproject mysite .
- confirm the generated
settings.py
doesn't mention DEFAULT_AUTO_FIELD
- confirm the generated
- run though the tutorial, up to and including https://docs.djangoproject.com/en/3.2/intro/tutorial02/#creating-models
- attempt to
python manage.py makemigrations polls
- get errors
System check identified some issues: WARNINGS: polls.Choice: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the PollsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. polls.Question: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the PollsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'. Migrations for 'polls': polls/migrations/0001_initial.py - Create model Question - Create model Choice
Change History (3)
comment:1 by , 3 years ago
comment:2 by , 3 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
I cannot reproduce this issue, generated settings.py
contains DEFAULT_AUTO_FIELD
:
# Default primary key field type # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
it's also defined in polls/apps.py
:
class PollsConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'polls'
comment:3 by , 3 years ago
Ah, I see what I did wrong. I had a global Django installed so django-admin
was referencing the global 3.1.7 version instead of the 3.2.2 local version. Fixing that generated the right template project for me, showing the setting you see.
Sorry for the noise!
There are two ways that this could be solved:
settings.py
to addDEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
id = models.AutoField(primary_key=True)
for each model.I want to say the tutorial model update is better because it means not touching the template project, but I'm also not sure how important this default is with respect to having the default at start for new projects.