Opened 6 years ago

Closed 2 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: master
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@… 6 years ago.
model-instance-changes-v2.patch (2.7 KB) - added by seveas 6 years ago.
Better patch with docs and tests

Download all attachments as: .zip

Change History (13)

Changed 6 years ago by dennis@…

Changed 6 years ago by seveas

Better patch with docs and tests

comment:1 Changed 6 years ago by seveas

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

New patch, includes tests and documentation.

comment:2 Changed 6 years ago by Alex

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 6 years ago by kmike84@…

  • Cc kmike84@… added

comment:4 follow-up: Changed 5 years ago by thebitguru

  • 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.

comment:5 in reply to: ↑ 4 Changed 5 years ago by lukeplant

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 follow-up: Changed 5 years ago by lukeplant

  • 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 in reply to: ↑ 6 Changed 5 years ago by thebitguru

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 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to New feature

comment:9 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:10 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:11 Changed 2 years ago by akaariai

  • Resolution set to wontfix
  • Status changed from new to 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.

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