Opened 6 years ago

Closed 6 years ago

#18044 closed New feature (duplicate)

Efficient update query on

Reported by: Andrei Antoukh Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Django, by default saves all fields of the object on call save() method. I've seen that with slightly larger models, is quite inefficient.

I implemented this patch, in my case solves this problem. Would be nice, that django has this functionality by default.I have seen this issue, which appears to be related. I have seen the implementation and seen that they might be trying to solve 2 things at once.

In my case, just keep the status attribute, and only keep those that have changed. Works with inheritance 2 tables.This is a proposal. With pleasure I will write testcases for this, if is necessary.

Attachments (1)

django-efficient-save.patch (2.7 KB) - added by Andrei Antoukh 6 years ago.

Download all attachments as: .zip

Change History (3)

Changed 6 years ago by Andrei Antoukh

Attachment: django-efficient-save.patch added

comment:1 Changed 6 years ago by Andrei Antoukh

This is a related ticket: #4102

comment:2 Changed 6 years ago by Anssi Kääriäinen

Resolution: duplicate
Status: newclosed

There are two problems with the taken approach:

  • It will cause a noticeable slowdown to model.__init__, and thus fetching large resultsets will be slower
  • It might be backwards incompatible to update only change fields.

The correct approach in my opinion is to introduce a flag to, "only_fields", which allows you to control which fields to update. In addition allow custom model._state objects track which fields have been changed (basically, when a model is initialized by a query, call model._state.update(*init_fields)). Together these two allows creation of subclasses which allow model state tracking, and updating of only changed fields. There would be no backwards compatibility problems and there would be no performance problems if you do not use this approach.

In #4102 the automatic tracking of changed fields has been beaten to death. I am closing this ticket as a duplicate of #4102. Discussion of this should be continued in that ticket, or in django-developers mailing list. FWIW I do agree that the feature is useful. The first step is getting "only_fields" in.

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