#34242: Behavior of Model.delete() with regards to primary_key fields is not documented ------------------------------------------------+------------------------ Reporter: SwampFalc | Owner: nobody Type: Cleanup/optimization | Status: new Component: Documentation | Version: dev Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 1 UI/UX: 0 | ------------------------------------------------+------------------------ When .delete() is called on a model instance, the primary_key field of that instance is set to None (https://github.com/django/django/blob/main/django/db/models/deletion.py#L456).
For a "standard" autoincrement PK, this is fine. However, if you have defined an actual data field as primary_key on the model, this data is lost in the instance. This is counter to what is stated in the basic documentation about "Deleting objects" (https://docs.djangoproject.com/en/4.1/ref/models/instances/#deleting- objects): "This only deletes the object in the database; the Python instance will still exist and will still have data in its fields." There is also no mention of this in the documentation of "primary_key" (https://docs.djangoproject.com/en/4.1/ref/models/fields/#primary-key) or the more in-depth documentation about "Deleting objects" (https://docs.djangoproject.com/en/4.1/topics/db/queries/#deleting- objects). I feel that this should at the very least be mentioned in the "primary_key" documentation, perhaps in some of the other locations too. If an agreement can be reached about where it should be mentioned and where not, I am willing to create a pull request. -- Ticket URL: <https://code.djangoproject.com/ticket/34242> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/0107018581684583-ffae2763-b7d3-4b5d-840f-35fc41401e06-000000%40eu-central-1.amazonses.com.