Opened 3 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#37468 closed Cleanup/optimization (fixed)

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: Ready for checkin
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.

Change History (5)

comment:1 by Sarah Boyce, 3 weeks ago

Has patch: set

comment:2 by Jacob Walls, 3 weeks ago

Triage Stage: Unreviewed → Accepted

comment:3 by Jacob Walls, 3 weeks ago

Triage Stage: Accepted → Ready for checkin

comment:4 by Sarah Boyce <42296566+sarahboyce@…>, 3 weeks ago

Resolution: → fixed
Status: assigned → closed

In b501b84:

Fixed #37468 -- Clarified the effects of changing primary keys to existing values.

comment:5 by Sarah Boyce <42296566+sarahboyce@…>, 3 weeks ago

In 96ac744:

[6.1.x] Fixed #37468 -- Clarified the effects of changing primary keys to existing values.

Backport of b501b8493f31cf8cba31211d59b3b78642b8cb02 from main.

Note: See TracTickets for help on using tickets.
Back to Top