diff --git a/django_mongodb/fields/auto.py b/django_mongodb/fields/auto.py index 34dc3a449..9240c759b 100644 --- a/django_mongodb/fields/auto.py +++ b/django_mongodb/fields/auto.py @@ -34,6 +34,9 @@ def get_prep_value(self, value): def db_type(self, connection): return "objectId" + def rel_db_type(self, connection): + return "objectId" + def to_python(self, value): if value is None or isinstance(value, int): return value diff --git a/tests/queries_/__init__.py b/tests/queries_/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/queries_/models.py b/tests/queries_/models.py new file mode 100644 index 000000000..61b93890b --- /dev/null +++ b/tests/queries_/models.py @@ -0,0 +1,16 @@ +from django.db import models + + +class Author(models.Model): + name = models.CharField(max_length=10) + + def __str__(self): + return self.name + + +class Book(models.Model): + title = models.CharField(max_length=10) + author = models.ForeignKey(Author, models.CASCADE) + + def __str__(self): + return self.title diff --git a/tests/queries_/test_mql.py b/tests/queries_/test_mql.py new file mode 100644 index 000000000..d61e5839d --- /dev/null +++ b/tests/queries_/test_mql.py @@ -0,0 +1,26 @@ +from django.test import TestCase + +from .models import Author, Book + + +class MQLTests(TestCase): + def test_all(self): + with self.assertNumQueries(1) as ctx: + list(Author.objects.all()) + query = ctx.captured_queries[0]["sql"] + self.assertEqual(query, "db.queries__author.aggregate([{'$match': {'$expr': {}}}])") + + def test_join(self): + with self.assertNumQueries(1) as ctx: + list(Book.objects.filter(author__name="Bob")) + query = ctx.captured_queries[0]["sql"] + self.assertEqual( + query, + "db.queries__book.aggregate([" + "{'$lookup': {'from': 'queries__author', " + "'let': {'parent__field__0': '$author_id'}, " + "'pipeline': [{'$match': {'$expr': " + "{'$and': [{'$eq': ['$$parent__field__0', '$_id']}]}}}], 'as': 'queries__author'}}, " + "{'$unwind': '$queries__author'}, " + "{'$match': {'$expr': {'$eq': ['$queries__author.name', 'Bob']}}}])", + )