Kaydet (Commit) 5ca9cf47 authored tarafından Morgan Wahl's avatar Morgan Wahl Kaydeden (comit) Tim Graham

[2.0.x] Refs #28856 -- Fixed caching of a GenericForeignKey pointing to a model…

[2.0.x] Refs #28856 -- Fixed caching of a GenericForeignKey pointing to a model that uses more than one level of MTI.

Forwardport of 35222035 from stable/1.11.x
üst 6dea1b18
...@@ -234,7 +234,15 @@ class GenericForeignKey(FieldCacheMixin): ...@@ -234,7 +234,15 @@ class GenericForeignKey(FieldCacheMixin):
pk = rel_obj._meta.pk pk = rel_obj._meta.pk
# If the primary key is a remote field, use the referenced # If the primary key is a remote field, use the referenced
# field's to_python(). # field's to_python().
pk_to_python = pk.target_field.to_python if pk.remote_field else pk.to_python to_python_field = pk
# Out of an abundance of caution, avoid infinite loops.
seen = {to_python_field}
while to_python_field.remote_field:
to_python_field = to_python_field.target_field
if to_python_field in seen:
break
seen.add(to_python_field)
pk_to_python = to_python_field.to_python
if pk_to_python(pk_val) != rel_obj._get_pk_val(): if pk_to_python(pk_val) != rel_obj._get_pk_val():
rel_obj = None rel_obj = None
else: else:
......
...@@ -17,3 +17,7 @@ Bugfixes ...@@ -17,3 +17,7 @@ Bugfixes
* Fixed incorrect foreign key constraint name for models with quoted * Fixed incorrect foreign key constraint name for models with quoted
``db_table`` (:ticket:`28876`). ``db_table`` (:ticket:`28876`).
* Fixed a regression in caching of a ``GenericForeignKey`` when the referenced
model instance uses more than one level of multi-table inheritance
(:ticket:`28856`).
...@@ -17,3 +17,7 @@ Bugfixes ...@@ -17,3 +17,7 @@ Bugfixes
* Fixed incorrect foreign key constraint name for models with quoted * Fixed incorrect foreign key constraint name for models with quoted
``db_table`` (:ticket:`28876`). ``db_table`` (:ticket:`28876`).
* Fixed a regression in caching of a ``GenericForeignKey`` when the referenced
model instance uses more than one level of multi-table inheritance
(:ticket:`28856`).
...@@ -38,6 +38,11 @@ class Restaurant(Place): ...@@ -38,6 +38,11 @@ class Restaurant(Place):
return "Restaurant: %s" % self.name return "Restaurant: %s" % self.name
class Cafe(Restaurant):
def __str__(self):
return "Cafe: %s" % self.name
class Address(models.Model): class Address(models.Model):
street = models.CharField(max_length=80) street = models.CharField(max_length=80)
city = models.CharField(max_length=50) city = models.CharField(max_length=50)
......
...@@ -5,9 +5,10 @@ from django.forms.models import modelform_factory ...@@ -5,9 +5,10 @@ from django.forms.models import modelform_factory
from django.test import TestCase, skipIfDBFeature from django.test import TestCase, skipIfDBFeature
from .models import ( from .models import (
A, Address, B, Board, C, CharLink, Company, Contact, Content, D, Developer, A, Address, B, Board, C, Cafe, CharLink, Company, Contact, Content, D,
Guild, HasLinkThing, Link, Node, Note, OddRelation1, OddRelation2, Developer, Guild, HasLinkThing, Link, Node, Note, OddRelation1,
Organization, Person, Place, Related, Restaurant, Tag, Team, TextLink, OddRelation2, Organization, Person, Place, Related, Restaurant, Tag, Team,
TextLink,
) )
...@@ -53,6 +54,11 @@ class GenericRelationTests(TestCase): ...@@ -53,6 +54,11 @@ class GenericRelationTests(TestCase):
CharLink.objects.create(content_object=restaurant) CharLink.objects.create(content_object=restaurant)
charlink = CharLink.objects.latest('pk') charlink = CharLink.objects.latest('pk')
self.assertIs(charlink.content_object, charlink.content_object) self.assertIs(charlink.content_object, charlink.content_object)
# If the model (Cafe) uses more than one level of multi-table inheritance.
cafe = Cafe.objects.create()
CharLink.objects.create(content_object=cafe)
charlink = CharLink.objects.latest('pk')
self.assertIs(charlink.content_object, charlink.content_object)
def test_q_object_or(self): def test_q_object_or(self):
""" """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment