Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #18432 -- Prevented the ForeignKey field from creating an inval…

…id query when chained. Thanks, Jann Kleen.
  • Loading branch information...
commit 1a412dda621d8623abb91f8687f52de30a79901a 1 parent 5e6ded2
Jann Kleen authored June 07, 2012 jezdez committed June 07, 2012
2  django/db/models/fields/related.py
@@ -359,7 +359,7 @@ def __get__(self, instance, instance_type=None):
23  tests/regressiontests/model_regress/models.py
@@ -7,6 +7,7 @@
7 7
     (2, 'second'),
8 8
 )
9 9
 
  10
+
10 11
 class Article(models.Model):
11 12
     headline = models.CharField(max_length=100, default='Default headline')
12 13
     pub_date = models.DateTimeField()
@@ -15,24 +16,28 @@ class Article(models.Model):
15 16
     article_text = models.TextField()
16 17
 
17 18
     class Meta:
18  
-        ordering = ('pub_date','headline')
  19
+        ordering = ('pub_date', 'headline')
19 20
         # A utf-8 verbose name (Ångström's Articles) to test they are valid.
20 21
         verbose_name = "\xc3\x85ngstr\xc3\xb6m's Articles"
21 22
 
22 23
     def __unicode__(self):
23 24
         return self.headline
24 25
 
  26
+
25 27
 class Movie(models.Model):
26 28
     #5218: Test models with non-default primary keys / AutoFields
27 29
     movie_id = models.AutoField(primary_key=True)
28 30
     name = models.CharField(max_length=60)
29 31
 
  32
+
30 33
 class Party(models.Model):
31 34
     when = models.DateField(null=True)
32 35
 
  36
+
33 37
 class Event(models.Model):
34 38
     when = models.DateTimeField()
35 39
 
  40
+
36 41
 class Department(models.Model):
37 42
     id = models.PositiveIntegerField(primary_key=True)
38 43
     name = models.CharField(max_length=200)
@@ -40,6 +45,7 @@ class Department(models.Model):
40 45
     def __unicode__(self):
41 46
         return self.name
42 47
 
  48
+
43 49
 class Worker(models.Model):
44 50
     department = models.ForeignKey(Department)
45 51
     name = models.CharField(max_length=200)
@@ -47,6 +53,7 @@ class Worker(models.Model):
47 53
     def __unicode__(self):
48 54
         return self.name
49 55
 
  56
+
50 57
 class BrokenUnicodeMethod(models.Model):
51 58
     name = models.CharField(max_length=7)
52 59
 
@@ -55,5 +62,19 @@ def __unicode__(self):
55 62
         # object).
56 63
         return 'Názov: %s' % self.name
57 64
 
  65
+
58 66
 class NonAutoPK(models.Model):
59 67
     name = models.CharField(max_length=10, primary_key=True)
  68
+
  69
+
  70
+#18432: Chained foreign keys with to_field produce incorrect query
  71
+class Model1(models.Model):
  72
+    pkey = models.IntegerField(unique=True, db_index=True)
  73
+
  74
+
  75
+class Model2(models.Model):
  76
+    model1 = models.ForeignKey(Model1, unique=True, to_field='pkey')
  77
+
  78
+
  79
+class Model3(models.Model):
  80
+    model2 = models.ForeignKey(Model2, unique=True, to_field='model1')
18  tests/regressiontests/model_regress/tests.py
@@ -8,8 +8,7 @@
8 8
 from django.utils import tzinfo
9 9
 
10 10
 from .models import (Worker, Article, Party, Event, Department,
11  
-    BrokenUnicodeMethod, NonAutoPK)
12  
-
  11
+    BrokenUnicodeMethod, NonAutoPK, Model1, Model2, Model3)
13 12
 
14 13
 
15 14
 class ModelTests(TestCase):
@@ -47,7 +46,7 @@ def test_long_textfield(self):
47 46
         a = Article.objects.create(
48 47
             headline="Really, really big",
49 48
             pub_date=datetime.datetime.now(),
50  
-            article_text = "ABCDE" * 1000
  49
+            article_text="ABCDE" * 1000
51 50
         )
52 51
         a = Article.objects.get(pk=a.pk)
53 52
         self.assertEqual(len(a.article_text), 5000)
@@ -164,6 +163,19 @@ def test_timezones(self):
164 163
             1
165 164
         )
166 165
 
  166
+    def test_chained_fks(self):
  167
+        """
  168
+        Regression for #18432: Chained foreign keys with to_field produce incorrect query
  169
+        """
  170
+
  171
+        m1 = Model1.objects.create(pkey=1000)
  172
+        m2 = Model2.objects.create(model1=m1)
  173
+        m3 = Model3.objects.create(model2=m2)
  174
+
  175
+        # this is the actual test for #18432
  176
+        m3 = Model3.objects.get(model2=1000)
  177
+        m3.model2
  178
+
167 179
 
168 180
 class ModelValidationTest(TestCase):
169 181
     def test_pk_validation(self):

0 notes on commit 1a412dd

Please sign in to comment.
Something went wrong with that request. Please try again.