Opened 16 years ago
Closed 12 years ago
#11007 closed New feature (wontfix)
Add django.db.Model.changes(self) that returns a dict of differences
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | kmike84@…, farhan@… | Triage Stage: | Design decision needed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
The attached patch adds a method to the Model class that returns a dict of differences between the current instance and the values stored in the database.
Attachments (2)
Change History (13)
by , 16 years ago
Attachment: | modelchanges.patch added |
---|
by , 15 years ago
Attachment: | model-instance-changes-v2.patch added |
---|
comment:2 by , 15 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
comment:3 by , 15 years ago
Cc: | added |
---|
follow-up: 5 comment:4 by , 15 years ago
Cc: | added |
---|
Thanks for the code, this saved me some time and it would be awesome if it were a part of the official release. One thing to point out though, this doesn't account for m2m fields. Should that be included in these patches? I have needed that for my project and will post an updated patch once I have it tested.
comment:5 by , 15 years ago
Replying to thebitguru:
One thing to point out though, this doesn't account for m2m fields. Should that be included in these patches?
That's not possible, because m2m data is saved to the database independently of the objects own data e.g. via obj.othermodel_set.add()
calls.
follow-up: 7 comment:6 by , 15 years ago
Patch needs improvement: | set |
---|
The .endswith("_ptr")
checks could have some false positives, if the developer happened to name a field "foo_ptr", which is not forbidden IIRC.
comment:7 by , 15 years ago
Replying to lukeplant:
The
.endswith("_ptr")
checks could have some false positives, if the developer happened to name a field "foo_ptr", which is not forbidden IIRC.
Thanks, Luke. Would we want to use something like the following?
fields = [field.name for field in self._meta._fields() if not field.rel or (field.rel and not field.rel.parent_link)]
If so, then I can attach an updated patch.
Also, do we need tests for this functionality before it can be considered for the standard code?
comment:8 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:11 by , 12 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Hmmh, this might be API bloat... If inspection of model fields were part of the models API, then this would be somewhat straightforward to implement if needed in a project.
There doesn't seem to be too much demand for this feature, and we have managed to live without this for a long time. Even if this were to be implemented I think a better API would be obj.diff(another_obj), so that you could compare not just against current state and DB state, but against any other object you want.
So, I am closing this as wontfix.
Better patch with docs and tests