#28344: Add for_update parameter to Model.refresh_from_db()
-------------------------------------+-------------------------------------
     Reporter:  Patryk Zawadzki      |                    Owner:  (none)
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 I don't think it would be a problem for `refresh_from_db` to apply fields
 and `using` if explicitly provided with `from_queryset`


 {{{#!diff
 diff --git a/django/db/models/base.py b/django/db/models/base.py
 index 6eaa600f10..8a9cbd606e 100644
 --- a/django/db/models/base.py
 +++ b/django/db/models/base.py
 @@ -672,7 +672,7 @@ def get_deferred_fields(self):
              if f.attname not in self.__dict__
          }

 -    def refresh_from_db(self, using=None, fields=None):
 +    def refresh_from_db(self, using=None, fields=None,
 from_queryset=None):
          """
          Reload field values from the database.

 @@ -704,10 +704,15 @@ def refresh_from_db(self, using=None, fields=None):
                      "are not allowed in fields." % LOOKUP_SEP
                  )

 -        hints = {"instance": self}
 -        db_instance_qs = self.__class__._base_manager.db_manager(
 -            using, hints=hints
 -        ).filter(pk=self.pk)
 +        if from_queryset is not None:
 +            if using is not None:
 +                from_queryset = from_queryset.using(using)
 +        else:
 +            hints = {"instance": self}
 +            from_queryset = self.__class__._base_manager.db_manager(
 +                using, hints=hints
 +            )
 +        db_instance_qs = from_queryset.filter(pk=self.pk)

          # Use provided fields, if not set then reload all non-deferred
 fields.
          deferred_fields = self.get_deferred_fields()
 }}}

 That would mean that

 {{{#!python
 refresh_from_db(using='other',
 from_queryset=Author.objects.using('default'))  # would use `other`
 refresh_from_db(fields=['first_name'],
 from_queryset=Author.objects.only('last_name')) # would use ['first_name']

 Why do you think the addition of a `refresh_from_queryset` method is
 warranted?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28344#comment:33>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018c59be9828-ae7cb63a-8f97-40a8-841f-73109787ce03-000000%40eu-central-1.amazonses.com.

Reply via email to