Skip to content

Commit 7a03e3a

Browse files
authored
Merge branch 'main' into partial-indexes
2 parents 99623ee + a06efe3 commit 7a03e3a

File tree

7 files changed

+78
-5
lines changed

7 files changed

+78
-5
lines changed

.github/workflows/test-python.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ jobs:
9494
migrations
9595
model_fields
9696
model_forms
97+
model_formsets
9798
model_inheritance_regress
9899
mutually_referential
99100
nested_foreign_keys

django_mongodb/fields/auto.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ def __init__(self, *args, **kwargs):
1515
kwargs["db_column"] = "_id"
1616
super().__init__(*args, **kwargs)
1717

18+
def deconstruct(self):
19+
name, path, args, kwargs = super().deconstruct()
20+
if self.db_column == "_id":
21+
del kwargs["db_column"]
22+
if path.startswith("django_mongodb.fields.auto"):
23+
path = path.replace("django_mongodb.fields.auto", "django_mongodb.fields")
24+
return name, path, args, kwargs
25+
1826
def get_prep_value(self, value):
1927
if value is None:
2028
return None
@@ -34,17 +42,23 @@ def get_prep_value(self, value):
3442
def db_type(self, connection):
3543
return "objectId"
3644

45+
def rel_db_type(self, connection):
46+
return "objectId"
47+
3748
def to_python(self, value):
3849
if value is None or isinstance(value, int):
3950
return value
4051
try:
4152
return ObjectId(value)
4253
except errors.InvalidId:
43-
raise exceptions.ValidationError(
44-
self.error_messages["invalid"],
45-
code="invalid",
46-
params={"value": value},
47-
) from None
54+
try:
55+
return int(value)
56+
except ValueError:
57+
raise exceptions.ValidationError(
58+
self.error_messages["invalid"],
59+
code="invalid",
60+
params={"value": value},
61+
) from None
4862

4963
@cached_property
5064
def validators(self):

tests/model_fields_/__init__.py

Whitespace-only changes.

tests/model_fields_/test_autofield.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.test import SimpleTestCase
2+
3+
from django_mongodb.fields import ObjectIdAutoField
4+
5+
6+
class MethodTests(SimpleTestCase):
7+
def test_deconstruct(self):
8+
field = ObjectIdAutoField()
9+
name, path, args, kwargs = field.deconstruct()
10+
self.assertEqual(path, "django_mongodb.fields.ObjectIdAutoField")
11+
self.assertEqual(args, [])
12+
self.assertEqual(kwargs, {"primary_key": True})
13+
14+
def test_to_python(self):
15+
f = ObjectIdAutoField()
16+
self.assertEqual(f.to_python("1"), 1)

tests/queries_/__init__.py

Whitespace-only changes.

tests/queries_/models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.db import models
2+
3+
4+
class Author(models.Model):
5+
name = models.CharField(max_length=10)
6+
7+
def __str__(self):
8+
return self.name
9+
10+
11+
class Book(models.Model):
12+
title = models.CharField(max_length=10)
13+
author = models.ForeignKey(Author, models.CASCADE)
14+
15+
def __str__(self):
16+
return self.title

tests/queries_/test_mql.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from django.test import TestCase
2+
3+
from .models import Author, Book
4+
5+
6+
class MQLTests(TestCase):
7+
def test_all(self):
8+
with self.assertNumQueries(1) as ctx:
9+
list(Author.objects.all())
10+
query = ctx.captured_queries[0]["sql"]
11+
self.assertEqual(query, "db.queries__author.aggregate([{'$match': {'$expr': {}}}])")
12+
13+
def test_join(self):
14+
with self.assertNumQueries(1) as ctx:
15+
list(Book.objects.filter(author__name="Bob"))
16+
query = ctx.captured_queries[0]["sql"]
17+
self.assertEqual(
18+
query,
19+
"db.queries__book.aggregate(["
20+
"{'$lookup': {'from': 'queries__author', "
21+
"'let': {'parent__field__0': '$author_id'}, "
22+
"'pipeline': [{'$match': {'$expr': "
23+
"{'$and': [{'$eq': ['$$parent__field__0', '$_id']}]}}}], 'as': 'queries__author'}}, "
24+
"{'$unwind': '$queries__author'}, "
25+
"{'$match': {'$expr': {'$eq': ['$queries__author.name', 'Bob']}}}])",
26+
)

0 commit comments

Comments
 (0)