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: dennis@… 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)

modelchanges.patch (848 bytes ) - added by dennis@… 16 years ago.
model-instance-changes-v2.patch (2.7 KB ) - added by Dennis Kaarsemaker 15 years ago.
Better patch with docs and tests

Download all attachments as: .zip

Change History (13)

by dennis@…, 16 years ago

Attachment: modelchanges.patch added

by Dennis Kaarsemaker, 15 years ago

Better patch with docs and tests

comment:1 by Dennis Kaarsemaker, 15 years ago

New patch, includes tests and documentation.

comment:2 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedDesign decision needed

comment:3 by kmike84@…, 15 years ago

Cc: kmike84@… added

comment:4 by Farhan Ahmad, 15 years ago

Cc: farhan@… 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.

in reply to:  4 comment:5 by Luke Plant, 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.

comment:6 by Luke Plant, 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.

in reply to:  6 comment:7 by Farhan Ahmad, 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 Julien Phalip, 13 years ago

Severity: Normal
Type: New feature

comment:9 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:10 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:11 by Anssi Kääriäinen, 12 years ago

Resolution: wontfix
Status: newclosed

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.

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