#34137: model.refresh_from_db() doesn't  clear cached generic foreign keys
--------------------------------------+------------------------------------
     Reporter:  pascal chambon        |                    Owner:  nobody
         Type:  Bug                   |                   Status:  new
    Component:  contrib.contenttypes  |                  Version:  3.2
     Severity:  Normal                |               Resolution:
     Keywords:                        |             Triage Stage:  Accepted
    Has patch:  0                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------

Comment (by Simon Charette):

 Bonjour Pascal,

 It seems to be an oversight in `Model.refresh_from_db` as it should also
 consider `_meta.private_fields` which is where `GenericForeignKey` and
 `GenericRel` end up as opposed to `related_objects`. Something along these
 lines should address the issue

 {{{#!diff
 diff --git a/django/db/models/base.py b/django/db/models/base.py
 index 2eb7ba7e9b..0f5f8d0881 100644
 --- a/django/db/models/base.py
 +++ b/django/db/models/base.py
 @@ -737,6 +737,11 @@ def refresh_from_db(self, using=None, fields=None):
              if field.is_cached(self):
                  field.delete_cached_value(self)

 +        # Clear cached private relations.
 +        for field in self._meta.private_fields:
 +            if field.is_relation and field.is_cached(self):
 +                field.delete_cached_value(self)
 +
          self._state.db = db_instance._state.db

      async def arefresh_from_db(self, using=None, fields=None):
 }}}

 Would you be interested in submitting a patch whit these changes and
 
[https://github.com/django/django/blob/3dc9f3ac6960c83cd32058677eb0ddb5a5e5da43/tests/contenttypes_tests/test_fields.py#L12-L44
 adding a regression test to the suite]?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34137#comment:3>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701843ea9c02d-c126805c-b4a4-4b85-8b43-74b4b81be0f3-000000%40eu-central-1.amazonses.com.

Reply via email to