#27564: `refresh_from_db()` should return the model instance instead of None
-------------------------------------+-------------------------------------
     Reporter:  Tom                  |                    Owner:  nobody
         Type:                       |                   Status:  closed
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  1.10
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  wontfix
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Aymeric Augustin):

 In fact the reason why `refresh_from_db` exists is **not** to return an
 updated object, but instead to refresh it in place so that other pieces of
 code that have kept a reference to this instance see the updated data.

 To return a copy, `Thing.objects.get(pk=thing.pk)` is hardly longer than
 `thing.refresh_from_db()` and (in my opinion) should be preferred because
 it's explicit.

 A shorter and more idiomatic way to write:
 {{{
 assert all(model.refresh_from_db().status == Something for model in
 queryset)
 }}}
 is
 {{{
 assert all(model.status == Something for model in queryset.all())
 }}}

 (The `.all()` creates a new queryset that forces a new database query.)

 If `refresh_from_db()` returns the updated instance, it will be used in
 situations when it isn't necessary and where the regular, safer APIs that
 don't involve in place mutations are a better choice.

--
Ticket URL: <https://code.djangoproject.com/ticket/27564#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/061.d1c2012ed3eeaf426cdd20a94062544a%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to