瀏覽代碼

Refs #29789 -- Added more tests for FilteredRelation with condition outside of relation name.

Francesco Panico 1 年之前
父節點
當前提交
e4a5527d1d
共有 1 個文件被更改,包括 22 次插入0 次删除
  1. 22 0
      tests/filtered_relation/tests.py

+ 22 - 0
tests/filtered_relation/tests.py

@@ -11,8 +11,10 @@ from django.db.models import (
     FilteredRelation,
     Q,
     Sum,
+    Value,
     When,
 )
+from django.db.models.functions import Concat
 from django.test import TestCase
 from django.test.testcases import skipUnlessDBFeature
 
@@ -629,6 +631,26 @@ class FilteredRelationTests(TestCase):
                 ),
             )
 
+    def test_condition_with_exact_lookup_outside_relation_name(self):
+        qs = Author.objects.annotate(
+            book_editor=FilteredRelation(
+                "book__editor",
+                condition=Q(book__author__name="book"),
+            ),
+        ).filter(book_editor__isnull=True)
+        self.assertEqual(qs.count(), 4)
+
+    def test_condition_with_func_and_lookup_outside_relation_name(self):
+        qs = Author.objects.annotate(
+            book_editor=FilteredRelation(
+                "book__editor",
+                condition=Q(
+                    book__title=Concat(Value("The book by "), F("book__author__name"))
+                ),
+            ),
+        ).filter(book_editor__isnull=False)
+        self.assertEquals(qs.count(), 1)
+
     def test_condition_deeper_relation_name(self):
         msg = (
             "FilteredRelation's condition doesn't support nested relations "