#25160 closed Cleanup/optimization (fixed)
Move unsaved model instance assignment check to model.save()
Reported by: | Tim Graham | Owned by: | Aymeric Augustin |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.8 |
Severity: | Normal | Keywords: | |
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
As proposed in the last comments of #10811, some people wish to use the ORM with unsaved model instances so it's been proposed to move the data loss check to the model.save()
method. Such a solution would require additional consideration for bulk_create()
since save()
isn't called in that scenario. I've attached an initial patch to give an idea of the changes required.
Attachments (1)
Change History (17)
by , 9 years ago
Attachment: | 25160.diff added |
---|
comment:1 by , 9 years ago
See 5643a3b51be338196d0b292d5626ad43648448d3 for the original commit and 81e1a35c364e5353d2bf99368ad30a4184fbb653 which could be reverted.
comment:2 by , 9 years ago
+1 to this change. I think the ability to use ORM model instances purely in memory is important for testing. In my own upgrade to 1.8 I simply disabled the new check by switching all my models to use a ForeignKey with the bypass class attribute set, which was a pain.
comment:3 by , 9 years ago
Since the purpose of bulk_create
is speed I think it may be preferable to just not have this check there than to check all instances. It could be benchmarked to see whether adding the check slows it down noticeably.
follow-up: 5 comment:4 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
I'll keep working on this. Carl, are you in favor of a backport to 1.8 as Anssi was in the linked ticket?
comment:5 by , 9 years ago
Replying to timgraham:
I'll keep working on this. Carl, are you in favor of a backport to 1.8 as Anssi was in the linked ticket?
I think so. The original change was a backwards-incompatible regression, which should have had a deprecation path at the very least, if we even wanted to prohibit all in-memory use (which I don't think we should). So I think that regression justifies a backported fix.
comment:6 by , 9 years ago
+1 to this change as well. I discovered we were using related model objects for unsaved data while upgrading an old project and stumbled through several different options as workarounds before eventually settling on this: https://www.caktusgroup.com/blog/2015/07/28/using-unsaved-related-models-sample-data-django-18/
So, if you don't happen to backport to 1.8, there is a workaround, but in the end I do think it makes a lot more sense to validate this in save() rather then at the time of assignment.
comment:8 by , 9 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:11 by , 9 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
For consistency, I think we should make the same change on reverse relations.
comment:12 by , 9 years ago
Has patch: | unset |
---|---|
Owner: | changed from | to
Status: | new → assigned |
Triage Stage: | Ready for checkin → Accepted |
comment:14 by , 9 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
initial work