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):
7777 # Connection creation doesn't follow the usual Django API.
7878 "backends.tests.ThreadTests.test_pass_connection_between_threads" ,
7979 "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" ,
8084 # Object of type ObjectId is not JSON serializable.
8185 "auth_tests.test_views.LoginTest.test_login_session_without_hash_session_key" ,
8286 # GenericRelation.value_to_string() assumes integer pk.
Original file line number Diff line number Diff line change 11from django .db import models
22
3- from django_mongodb .fields import ObjectIdField
3+ from django_mongodb .fields import ObjectIdAutoField , ObjectIdField
44
55
66class Author (models .Model ):
@@ -31,3 +31,25 @@ class Tag(models.Model):
3131
3232 def __str__ (self ):
3333 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 11from bson import ObjectId
22from django .test import TestCase
33
4- from .models import Tag
4+ from .models import Order , OrderItem , Tag
55
66
77class ObjectIdTests (TestCase ):
@@ -96,3 +96,20 @@ def test_invalid_object_id(self):
9696 msg = f"Field 'group_id' expected an ObjectId but got '{ value } '."
9797 with self .assertRaisesMessage (ValueError , msg ):
9898 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