8000 Fixed #36207 -- Cleared cached ForeignObject relations via refresh_fr… · django/django@cec9568 · GitHub
[go: up one dir, main page]

Skip to content

Commit cec9568

Browse files
Fixed #36207 -- Cleared cached ForeignObject relations via refresh_from_db().
1 parent 84e9126 commit cec9568

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

django/db/models/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,12 @@ def refresh_from_db(self, using=None, fields=None, from_queryset=None):
756756

757757
db_instance = db_instance_qs.get()
758758
non_loaded_fields = db_instance.get_deferred_fields()
759-
for field in self._meta.concrete_fields:
759+
for field in< 8000 /span> self._meta.fields:
760760
if field.attname in non_loaded_fields:
761761
# This field wasn't refreshed - skip ahead.
762762
continue
763-
setattr(self, field.attname, getattr(db_instance, field.attname))
763+
if field.concrete:
764+
setattr(self, field.attname, getattr(db_instance, field.attname))
764765
# Clear or copy cached foreign keys.
765766
if field.is_relation:
766767
if field.is_cached(db_instance):

tests/foreign_object/tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,14 @@ def test_prefetch_related_m2m_reverse_works(self):
450450
normal_groups_lists = [list(p.groups.all()) for p in Person.objects.all()]
451451
self.assertEqual(groups_lists, normal_groups_lists)
452452

453+
def test_refresh_foreign_object(self):
454+
member = Membership.objects.create(
455+
membership_country=self.usa, person=self.bob, group=self.cia
456+
)
457+
member.person = self.jim
458+
member.refresh_from_db()
459+
self.assertEqual(member.person, self.bob)
460+
453461
@translation.override("fi")
454462
def test_translations(self):
455463
a1 = Article.objects.create(pub_date=datetime.date.today())

tests/model_meta/tests.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from django.apps import apps
22
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
3+
from django.contrib.contenttypes.models import ContentType
34
from django.core.exceptions import FieldDoesNotExist
45
from django.db.models import CharField, Field, ForeignObjectRel, ManyToManyField
56
from django.db.models.options import EMPTY_RELATION_TREE, IMMUTABLE_WARNING
6-
from django.test import SimpleTestCase, override_settings
7+
from django.test import SimpleTestCase, TestCase, override_settings
78

89
from .models import (
910
AbstractPerson,
@@ -386,3 +387,23 @@ def test_abstract_model_not_instantiated(self):
386387
msg = "Abstract models cannot be instantiated."
387388
with self.assertRaisesMessage(TypeError, msg):
388389
AbstractPerson()
390+
391+
392+
class RefreshTests(TestCase):
393+
def test_refresh_from_db(self):
394+
relation = Relation.objects.create()
395+
content_type = ContentType.objects.create()
396+
person = Person.objects.create(
397+
fk_inherited=relation,
398+
fk_abstract=relation,
399+
content_type_base=content_type,
400+
content_type_abstract=content_type,
401+
content_type_concrete=content_type,
402+
object_id_base=1,
403+
object_id_abstract=1,
404+
object_id_concrete=1,
405+
fk_base=relation,
406+
)
407+
408+
with self.assertNumQueries(1):
409+
person.refresh_from_db()

0 commit comments

Comments
 (0)
0