Opened 57 minutes ago
Last modified 15 minutes ago
#37468 assigned Cleanup/optimization
Clarify the effects of changing primary keys in models and ModelForms
| Reported by: | Sarah Boyce | Owned by: | Sarah Boyce |
|---|---|---|---|
| Component: | Documentation | Version: | 6.1 |
| Severity: | Normal | Keywords: | not-security |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
The primary key documentation explains that changing an existing instance’s primary key and saving it creates a new object alongside the original. See https://docs.djangoproject.com/en/dev/topics/db/models/#field-options
However, it does not explain that if an object with the new primary key already exists, saving the instance updates that object instead.
This behavior also applies to a ModelForm bound to an existing instance when an editable primary key field is included in the form. Changing that field can cause the form to update a different object from the one originally supplied through instance.
We should clarify this existing behavior by:
- Extending the primary key example to demonstrate updating an object that already has the newly assigned primary key. A model with an additional, non-primary-key field would help illustrate which data is updated.
- Expanding Selecting the fields to use to explain the consequences of including an editable primary key field, with a link to the model documentation. Applications should exclude this field unless users are intended to be able to change it.