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.
Comment (by SwampFalc):
You know, thinking about it some more... What exactly is the reason for
setting the PK to None upon deletion? Is that really necessary?
Should it perhaps only happen when the PK is an autoincrement? Or should
it perhaps not happen at all?
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:1>
* owner: nobody => santhosh_reddy
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:2>
* owner: santhosh_reddy => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:3>
* stage: Unreviewed => Accepted
Comment:
I think something as small as... "the Python instance will still exist and
will still have data in its fields, but the primary key is set to `None`"
in the [https://docs.djangoproject.com/en/4.1/ref/models/instances
/#deleting-objects Deleting objects] docs you point to, would likely be
enough.
> You know, thinking about it some more... What exactly is the reason for
setting the PK to None upon deletion? Is that really necessary?
Standardly, whether the object has a PK is the test whether it's been
saved (i.e. exists in the DB). I doubt that's changeable.
(Others may give a better account.)
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:2>
* owner: nobody => AshirRashid
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:3>
* owner: Ashir Rashid => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:4>
* owner: nobody => noFFENSE
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:3>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:4>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:5>
Comment (by noFFENSE):
Please, tell me if something is wrong, and I will fix it, that's only my
2nd contribution, so I do not know whether I did a good job or not
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:6>
* stage: Ready for checkin => Accepted
Comment:
Please don't mark your own PRs as "Ready for checkin".
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:7>
Comment (by noFFENSE):
Replying to [comment:7 Mariusz Felisiak]:
> Please don't mark your own PRs as "Ready for check-in".
Okay, may I ask why?
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:8>
Comment (by Carlton Gibson):
Have a read of the
[https://docs.djangoproject.com/en/dev/internals/contributing/triaging-
tickets/#triage-workflow Triage Workflow docs] — they explain the process.
(Basically, someone **else** needs to review your patch first.)
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:9>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:10>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"5cd1385356d4b275133ddb0f8c78b2f37c5901eb" 5cd13853]:
{{{
#!CommitTicketReference repository=""
revision="5cd1385356d4b275133ddb0f8c78b2f37c5901eb"
Fixed #34242 -- Doc'd that primary key is set to None when deleting
objects.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:11>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"fa52bb77747be33dc9dfb07c26307e60958adaaa" fa52bb7]:
{{{
#!CommitTicketReference repository=""
revision="fa52bb77747be33dc9dfb07c26307e60958adaaa"
[4.2.x] Fixed #34242 -- Doc'd that primary key is set to None when
deleting objects.
Backport of 5cd1385356d4b275133ddb0f8c78b2f37c5901eb from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:12>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"2a32d3963c974b605050688dacfe2a6ae76f52e8" 2a32d39]:
{{{
#!CommitTicketReference repository=""
revision="2a32d3963c974b605050688dacfe2a6ae76f52e8"
[4.1.x] Fixed #34242 -- Doc'd that primary key is set to None when
deleting objects.
Backport of 5cd1385356d4b275133ddb0f8c78b2f37c5901eb from main
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34242#comment:13>