Skip to content

Commit 4669613

Browse files
committed
docs
1 parent 080a666 commit 4669613

File tree

4 files changed

+73
-27
lines changed

4 files changed

+73
-27
lines changed

django_mongodb/queryset.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ def raw_aggregate(self, pipeline, using=None):
1313
return RawQuerySet(pipeline, model=self.model, using=using)
1414

1515

16+
class RawQuerySet(BaseRawQuerySet):
17+
def __init__(self, pipeline, model=None, using=None):
18+
super().__init__(pipeline, model=model, using=using)
19+
self.query = RawQuery(pipeline, using=self.db, model=self.model)
20+
# Override the superclass's columns property which relies on PEP 249's
21+
# cursor.description. Instead, RawModelIterable will set the columns
22+
# based on the keys in the first result.
23+
self.columns = None
24+
25+
def iterator(self):
26+
yield from RawModelIterable(self)
27+
28+
1629
class RawQuery(BaseRawQuery):
1730
def __init__(self, pipeline, using, model):
1831
self.pipeline = pipeline
@@ -28,24 +41,11 @@ def __str__(self):
2841
return str(self.pipeline)
2942

3043

31-
class RawQuerySet(BaseRawQuerySet):
32-
def __init__(self, pipeline, model=None, using=None):
33-
super().__init__(pipeline, model=model, using=using)
34-
self.query = RawQuery(pipeline, using=self.db, model=self.model)
35-
# Override the superclass's columns property which relies on PEP 249's
36-
# cursor.description. Instead, RawModelIterable will set the columns
37-
# based on the keys in the first result.
38-
self.columns = None
39-
40-
def iterator(self):
41-
yield from RawModelIterable(self)
42-
43-
4444
class RawModelIterable(BaseRawModelIterable):
4545
def __iter__(self):
4646
"""
47-
This is mostly copied from the superclass except for the part that sets
48-
self.queryset.columns from the first document.
47+
This is copied from the superclass except for the part that sets
48+
self.queryset.columns from the first result.
4949
"""
5050
db = self.queryset.db
5151
query = self.queryset.query
@@ -55,13 +55,13 @@ def __iter__(self):
5555
try:
5656
# Get the columns from the first result.
5757
try:
58-
first_item = next(query_iterator)
58+
first_result = next(query_iterator)
5959
except StopIteration:
6060
# No results.
6161
return
62-
self.queryset.columns = list(first_item.keys())
62+
self.queryset.columns = list(first_result.keys())
6363
# Reset the iterator to include the first item.
64-
query_iterator = self._make_result(chain([first_item], query_iterator))
64+
query_iterator = self._make_result(chain([first_result], query_iterator))
6565
(
6666
model_init_names,
6767
model_init_pos,
@@ -88,5 +88,9 @@ def __iter__(self):
8888
query.cursor.close()
8989

9090
def _make_result(self, query):
91+
"""
92+
Convert documents (dictionaries) to tuples as expected by the rest
93+
of __iter__().
94+
"""
9195
for result in query:
92-
yield list(result.values())
96+
yield tuple(result.values())

docs/source/conf.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,25 @@
1717
# -- General configuration ---------------------------------------------------
1818
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
1919

20-
extensions = []
20+
# If true, the current module name will be prepended to all description
21+
# unit titles (such as .. function::).
22+
add_module_names = False
23+
24+
extensions = [
25+
"sphinx.ext.intersphinx",
26+
]
2127

2228
# templates_path = ["_templates"]
2329
exclude_patterns = []
2430

31+
intersphinx_mapping = {
32+
"pymongo": ("https://pymongo.readthedocs.io/en/stable/", None),
33+
"python": ("https://docs.python.org/3/", None),
34+
"django": (
35+
"https://docs.djangoproject.com/en/5.0/",
36+
"http://docs.djangoproject.com/en/5.0/_objects/",
37+
),
38+
}
2539

2640
# -- Options for HTML output -------------------------------------------------
2741
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

docs/source/index.rst

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
.. django_mongodb documentation master file, created by
2-
sphinx-quickstart on Mon Apr 15 12:38:26 2024.
3-
You can adapt this file completely to your liking, but it should at least
4-
contain the root ``toctree`` directive.
5-
61
Welcome to django_mongodb's documentation!
72
==========================================
83

94
.. toctree::
10-
:maxdepth: 2
5+
:maxdepth: 1
116
:caption: Contents:
127

13-
8+
querysets
149

1510
Indices and tables
1611
==================

docs/source/querysets.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
``QuerySet`` API reference
2+
==========================
3+
4+
Some MongoDB-specific ``QuerySet`` methods are available by adding a custom
5+
:class:`~django.db.models.Manager`, ``MongoManager``, to your model::
6+
7+
from django.db import models
8+
9+
from django_mongodb.managers import MongoManager
10+
11+
12+
class MyModel(models.Model):
13+
...
14+
15+
objects = MongoManager()
16+
17+
18+
.. currentmodule:: django_mongodb.queryset.MongoQuerySet
19+
20+
``raw_aggregate()``
21+
-------------------
22+
23+
.. method:: raw_aggregate(pipeline, using=None)
24+
25+
Similar to :meth:`QuerySet.raw()<django.db.models.query.QuerySet.raw>`, but
26+
instead of a raw SQL query, this method accepts a pipeline that will be passed
27+
to :meth:`pymongo.collection.Collection.aggregate`. The pipeline must return
28+
the necessary fields to construct model instances and may also return
29+
additional fields that will be added as annotations on the models.
30+
31+
For example, you could write a custom match criteria::
32+
33+
Post.objects.raw_aggregate([{"$match": {"title": "MongoDB is fun!"}}])

0 commit comments

Comments
 (0)