Django

Code

Ticket #3182 (closed: wontfix)

Opened 3 years ago

Last modified 3 months ago

model instance update() method and QuerySet update_or_create() method

Reported by: Gary Wilson <gary.wilson@gmail.com> Assigned to: gwilson
Milestone: 1.1 beta Component: Database layer (models, ORM)
Version: SVN Keywords:
Cc: gary.wilson@gmail.com, real.human@mrmachine.net, alexkoshelev Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Method would call get_or_create(), updating the object's data if the object already existed.

Idea came up in:

http://groups.google.com/group/django-developers/browse_thread/thread/49a6b99dbcea4364/

Attachments

update_or_create.diff (1.4 kB) - added by Gary Wilson <gary.wilson@gmail.com> on 12/22/06 00:57:50.
update_or_create() methods for QuerySet? and Manager
update_or_create2.diff (7.0 kB) - added by Gary Wilson <gary.wilson@gmail.com> on 12/24/06 16:49:14.
added documentation and tests
update_or_create3.diff (7.0 kB) - added by Gary Wilson <gary.wilson@gmail.com> on 12/24/06 23:01:29.
removed the "required" wording in the documentation
update_and_update_or_create.diff (11.5 kB) - added by Gary Wilson <gary.wilson@gmail.com> on 12/26/06 00:07:07.
update() from #3180 and update_or_create() together
3182-update_or_create-r9844.diff (12.1 kB) - added by mrmachine on 02/17/09 18:37:41.
Updated to work with r9844.
3182.diff (12.3 kB) - added by gwilson on 03/15/09 12:08:40.
took save() out of the assignment loop in update() and made a few tweaks to the docs.
3182.2.diff (12.3 kB) - added by gwilson on 03/15/09 12:11:17.
added versionadded directive for update_or_create

Change History

12/22/06 00:57:25 changed by Gary Wilson <gary.wilson@gmail.com>

  • cc set to gary.wilson@gmail.com.
  • summary changed from update_or_create() QuerySet method to [patch] update_or_create() QuerySet method.

12/22/06 00:57:50 changed by Gary Wilson <gary.wilson@gmail.com>

  • attachment update_or_create.diff added.

update_or_create() methods for QuerySet? and Manager

12/22/06 01:05:22 changed by Gary Wilson <gary.wilson@gmail.com>

I should note that this patch depends on the update() method in #3181.

12/24/06 16:49:14 changed by Gary Wilson <gary.wilson@gmail.com>

  • attachment update_or_create2.diff added.

added documentation and tests

12/24/06 23:01:29 changed by Gary Wilson <gary.wilson@gmail.com>

  • attachment update_or_create3.diff added.

removed the "required" wording in the documentation

12/24/06 23:16:58 changed by Gary Wilson <gary.wilson@gmail.com>

Correction: I should note that this patch depends on the update() method in #3180.

12/26/06 00:07:07 changed by Gary Wilson <gary.wilson@gmail.com>

  • attachment update_and_update_or_create.diff added.

update() from #3180 and update_or_create() together

01/06/07 10:02:22 changed by anonymous

  • priority changed from normal to highest.

01/14/07 20:47:51 changed by Gary Wilson <gary.wilson@gmail.com>

  • priority changed from highest to normal.
  • summary changed from [patch] update_or_create() QuerySet method to [patch] model instance update() method and QuerySet update_or_create() method.

Changed the title to reflect the merging of the two patches.

01/17/07 20:37:21 changed by Gary Wilson <gary.wilson@gmail.com>

  • summary changed from [patch] model instance update() method and QuerySet update_or_create() method to model instance update() method and QuerySet update_or_create() method.
  • stage changed from Unreviewed to Ready for checkin.

As per Jacob's comments in the mailing list.

09/10/07 22:07:52 changed by gwilson

  • needs_better_patch set to 1.
  • stage changed from Ready for checkin to Accepted.

Model instance update method should not use __dict__.update() as that won't work with fields like ForeignKey. Perhaps we need to loop through all the fields, using setattr and the other logic present in Model.__init__().

02/17/09 18:37:41 changed by mrmachine

  • attachment 3182-update_or_create-r9844.diff added.

Updated to work with r9844.

02/17/09 18:50:32 changed by mrmachine

  • cc changed from gary.wilson@gmail.com to gary.wilson@gmail.com, real.human@mrmachine.net.
  • needs_better_patch deleted.
  • version set to SVN.

Updated the patch to work with trunk (r9844). Updated docs to match new docs. Updated the Model.update() method to use setattr() as mentioned above so it works with ForeignKey fields. Also updated a few typos in the existing QuerySet.update() tests and model instance docs.

This is listed as a maybe / "second-tier" feature for 1.1 with a comment that they are likely to be included if done. I think this should be ready to go in now.

02/25/09 21:13:51 changed by mrmachine

  • milestone set to 1.1 beta.

Changed milestone to 1.1 beta, because this is listed as a 1.1 maybe feature that should be included if done, and it appears to be done.

03/15/09 10:48:55 changed by anonymous

  • owner changed from nobody to anonymous.
  • status changed from new to assigned.

03/15/09 10:49:18 changed by gwilson

  • owner changed from anonymous to gwilson.
  • status changed from assigned to new.

03/15/09 12:08:40 changed by gwilson

  • attachment 3182.diff added.

took save() out of the assignment loop in update() and made a few tweaks to the docs.

03/15/09 12:11:17 changed by gwilson

  • attachment 3182.2.diff added.

added versionadded directive for update_or_create

03/22/09 06:15:14 changed by alexkoshelev

  • cc changed from gary.wilson@gmail.com, real.human@mrmachine.net to gary.wilson@gmail.com, real.human@mrmachine.net, alexkoshelev.

03/31/09 13:24:03 changed by jacob

  • status changed from new to closed.
  • resolution set to wontfix.

OK, this has gone around a few times on django-dev and in person, and it's clear that we're not reaching a consensus. There's a lot apathy, mostly -- few are arguing strongly for this feature, and few are arguing strongly against it. So, I'm going to do the BDFL thing and make a decision:

It's pretty clear to me that update() is a nonstarter; it's a single line of code::

def update(self, **kw):

self.objects.filter(pk=self.pk).update(**kw)

So that's out.

How about update_or_create()? I personally find the name counter-intuitive -- it's really get_and_then_update_or_create() -- but that's trivial; a better name could be found. A bigger problem is that it's nonatomic; we went to great lengths to insure that get_or_create() doesn't suffer from synchronization problems, but we can't do that here.

Given both these problems, and given that they won't exist if people are forced to write these methods in their own code, I'm marking this wontfix.


Add/Change #3182 (model instance update() method and QuerySet update_or_create() method)




Change Properties
Action