Opened 4 months ago
Closed 3 months ago
#35623 closed Cleanup/optimization (fixed)
Document that fields cannot be named 'check'
Reported by: | Francisco Camerini | Owned by: | Mohammad Salehi |
---|---|---|---|
Component: | Core (System checks) | Version: | 4.0 |
Severity: | Normal | Keywords: | models.E020 |
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
I've been tasked with migrating a large codebase from Django 3.2 to 4.2.
Upon updating to 4.0, I noticed that the models.E020 system check was being triggered.
This project has had multiple models with "check" fields for many years. This error was not being triggered before 4.0 because class methods were protected from being overwritten by fields. This behavior was changed here
This commit's message is very explicit about "check" being a reserved word that cannot be redefined as anything else other than a method. However, the documentation on field name restrictions is silent about that, only stating that:
- A field name cannot be a Python reserved word, because that would result in a Python syntax error.
- A field name cannot contain more than one underscore in a row, due to the way Django’s query lookup syntax works.
- A field name cannot end with an underscore.
For now I've silenced the models.E020 error, but this is far from ideal, what would be the best way to still run all model system checks without having to change our models to use a different field name?
Change History (7)
comment:1 by , 4 months ago
Summary: | Addressing models.E020 Error: Migration strategies for codebases with 'check' fields → Document that fields cannot be named 'check' |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Cleanup/optimization |
comment:2 by , 3 months ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:3 by , 3 months ago
Has patch: | set |
---|
comment:5 by , 3 months ago
Patch needs improvement: | set |
---|
comment:6 by , 3 months ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
For context, this check was introduced in Django 1.7.1. I agree that fields are not allowed to be called
check
should be added to the documentation on field names 👍I am going to slightly repurpose and accept this ticket for this.
Replying to Francisco Camerini:
I am not sure there is another option. I think that this check wasn't being raised before might have been down to the model hierarchy structure (hard to tell without seeing the models - refs #30427, #16176). You may have to update the field names and set
db_column="check"
. You might get more support if you were to ask this on one of our support channels