File tree Expand file tree Collapse file tree 3 files changed +45
-2
lines changed Expand file tree Collapse file tree 3 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -77,6 +77,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
77
77
# Connection creation doesn't follow the usual Django API.
78
78
"backends.tests.ThreadTests.test_pass_connection_between_threads" ,
79
79
"backends.tests.ThreadTests.test_default_connection_thread_local" ,
80
+ # ObjectId type mismatch in a subquery:
81
+ # https://github.com/mongodb-labs/django-mongodb/issues/161
82
+ "queries.tests.RelatedLookupTypeTests.test_values_queryset_lookup" ,
83
+ "queries.tests.ValuesSubqueryTests.test_values_in_subquery" ,
80
84
# Object of type ObjectId is not JSON serializable.
81
85
"auth_tests.test_views.LoginTest.test_login_session_without_hash_session_key" ,
82
86
# GenericRelation.value_to_string() assumes integer pk.
Original file line number Diff line number Diff line change 1
1
from django .db import models
2
2
3
- from django_mongodb .fields import ObjectIdField
3
+ from django_mongodb .fields import ObjectIdAutoField , ObjectIdField
4
4
5
5
6
6
class Author (models .Model ):
@@ -31,3 +31,25 @@ class Tag(models.Model):
31
31
32
32
def __str__ (self ):
33
33
return self .name
34
+
35
+
36
+ class Order (models .Model ):
37
+ id = ObjectIdAutoField (primary_key = True )
38
+ name = models .CharField (max_length = 12 , null = True , default = "" )
39
+
40
+ class Meta :
41
+ ordering = ("pk" ,)
42
+
43
+ def __str__ (self ):
44
+ return str (self .pk )
45
+
46
+
47
+ class OrderItem (models .Model ):
48
+ order = models .ForeignKey (Order , models .CASCADE , related_name = "items" )
49
+ status = ObjectIdField (null = True )
50
+
51
+ class Meta :
52
+ ordering = ("pk" ,)
53
+
54
+ def __str__ (self ):
55
+ return str (self .pk )
Original file line number Diff line number Diff line change 1
1
from bson import ObjectId
2
2
from django .test import TestCase
3
3
4
- from .models import Tag
4
+ from .models import Order , OrderItem , Tag
5
5
6
6
7
7
class ObjectIdTests (TestCase ):
@@ -96,3 +96,20 @@ def test_invalid_object_id(self):
96
96
msg = f"Field 'group_id' expected an ObjectId but got '{ value } '."
97
97
with self .assertRaisesMessage (ValueError , msg ):
98
98
Tag .objects .filter (group_id = value )
99
+
100
+ def test_values_in_subquery (self ):
101
+ # If a values() queryset is used, then the given values
102
+ # will be used instead of forcing use of the relation's field.
103
+ o1 = Order .objects .create ()
104
+ o2 = Order .objects .create ()
105
+ oi1 = OrderItem .objects .create (order = o1 , status = None )
106
+ oi1 .status = oi1 .pk
107
+ oi1 .save ()
108
+ OrderItem .objects .create (order = o2 , status = None )
109
+
110
+ # The query below should match o1 as it has related order_item
111
+ # with id == status.
112
+ self .assertSequenceEqual (
113
+ Order .objects .filter (items__in = OrderItem .objects .values_list ("status" )),
114
+ [o1 ],
115
+ )
You can’t perform that action at this time.
0 commit comments