Skip to content

Commit ac265ce

Browse files
committed
doc query limitations + docs polish + todos
1 parent 51da0e9 commit ac265ce

File tree

5 files changed

+67
-34
lines changed

5 files changed

+67
-34
lines changed

django_mongodb_backend/features.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,12 @@ def _supports_transactions(self):
636636
@cached_property
637637
def supports_queryable_encryption(self):
638638
"""
639-
Queryable Encryption requires a MongoDB 8.0 or later replica set or sharded
640-
cluster, as well as MongoDB Atlas or Enterprise.
639+
For testing purposes, Queryable Encryption requires a MongoDB 8.0 or
640+
later replica set or sharded cluster, as well as MongoDB Atlas or
641+
Enterprise. This flag must not guard any non-test functionality since
642+
it would prevent MongoDB 7.0 from being used, which also supports
643+
Queryable Encryption. The models in tests/encryption_ aren't compatible
644+
with MongoDB 7.0 because {"queryType": "range"} being "rangePreview".
641645
"""
642646
self.connection.ensure_connection()
643647
build_info = self.connection.connection.admin.command("buildInfo")

docs/howto/queryable-encryption.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Encryption in your Django project.
1414
.. admonition:: MongoDB requirements
1515

1616
Queryable Encryption can be used with MongoDB replica sets or sharded
17-
clusters running version 8.0 or later. Standalone instances are not
17+
clusters running version 7.0 or later. Standalone instances are not
1818
supported. The :ref:`manual:qe-compatibility-reference` table summarizes
1919
which MongoDB server products support Queryable Encryption.
2020

docs/ref/django-admin.rst

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,12 @@ Available commands
2121

2222
.. django-admin:: showencryptedfieldsmap
2323

24-
This command shows the mapping of encrypted fields to attributes including
25-
data type, data keys and query types. Its output can be used to set the
26-
:ref:`encrypted_fields_map <qe-configuring-encrypted-fields-map>` argument
27-
in :class:`AutoEncryptionOpts
28-
<pymongo.encryption_options.AutoEncryptionOpts>`.
24+
This command generates output for includision in
25+
:class:`~pymongo.encryption_options.AutoEncryptionOpts`\'s
26+
``encrypted_fields_map`` argument.
27+
28+
See :ref:`qe-configuring-encrypted-fields-map`.
2929

3030
.. django-admin-option:: --database DATABASE
3131

3232
Specifies the database to use. Defaults to ``default``.
33-
34-
To show the encrypted fields map for a database named ``encrypted``, run:
35-
36-
.. code-block:: console
37-
38-
$ python manage.py showencryptedfieldsmap --database encrypted

docs/ref/settings.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Settings
33
========
44

5-
.. _queryable-encryption-settings:
6-
75
Queryable Encryption
86
====================
97

docs/topics/queryable-encryption.rst

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Queryable Encryption
55
.. versionadded:: 5.2.3
66

77
Once you have successfully set up MongoDB Queryable Encryption as described in
8-
:doc:`the installation guide </howto/queryable-encryption>`, you can start
9-
using encrypted fields in your Django models.
8+
:doc:`/howto/queryable-encryption`, you can start using encrypted fields in
9+
your Django models.
1010

1111
Encrypted fields
1212
================
@@ -59,6 +59,8 @@ Here are models based on the `Python Queryable Encryption Tutorial`_::
5959
Migrations
6060
----------
6161

62+
# TODO: this is coupled to the howto
63+
6264
Once you have defined your models, create migrations with:
6365

6466
.. code-block:: console
@@ -76,9 +78,14 @@ model data. The fields will automatically handle encryption and decryption,
7678
ensuring that :ref:`sensitive data is stored securely in the database
7779
<manual:qe-features-encryption-at-rest>`.
7880

81+
# TODO: once encrypted models are migrated, encrypted fields cannot be added or
82+
modified.
83+
7984
Routers
8085
-------
8186

87+
# TODO: this is redundant with the howto.
88+
8289
The example above requires a :ref:`database router
8390
<qe-configuring-database-routers-setting>` to direct operations on models with
8491
encrypted fields to the appropriate database. It also requires the use of a
@@ -96,16 +103,15 @@ Querying encrypted fields
96103

97104
In order to query encrypted fields, you must define the queryable encryption
98105
query type in the model field definition. For example, if you want to query the
99-
``ssn`` field for equality, you can define it as follows::
106+
``ssn`` field for equality, you can add the ``queries`` argument::
100107

101108
class PatientRecord(EmbeddedModel):
102109
ssn = EncryptedCharField(max_length=11, queries={"queryType": "equality"})
103-
billing = EncryptedEmbeddedModelField("Billing")
104-
bill_amount = models.DecimalField(max_digits=10, decimal_places=2)
105110

106-
Then you can perform a query like this:
111+
Then you can perform a equality query just like you would on a non-encrypted
112+
field:
107113

108-
.. code-block:: console
114+
.. code-block:: pycon
109115
110116
>>> patient = Patient.objects.get(patient_record__ssn="123-45-6789")
111117
>>> patient.name
@@ -116,13 +122,13 @@ Then you can perform a query like this:
116122
Available query types
117123
~~~~~~~~~~~~~~~~~~~~~
118124

119-
The ``queries`` option should be a dictionary that specifies the type of queries
120-
that can be performed on the field. Of the :ref:`available query types
121-
<manual:qe-fundamentals-encrypt-query>` Django MongoDB Backend currently
122-
supports:
125+
The ``queries`` option should be a dictionary that specifies the type of
126+
queries that can be performed on the field, as well as any query options. The
127+
:ref:`available query types <manual:qe-fundamentals-encrypt-query>` depend on
128+
your version of MongoDB.
123129

124-
- ``equality``
125-
- ``range``
130+
For example, in MongoDB 8.0, the supported types are ``equality`` and
131+
``range``.
126132

127133
.. admonition:: Query types vs. Django lookups
128134

@@ -131,10 +137,41 @@ supports:
131137
perform comparisons on encrypted fields, while Django's range lookups are
132138
used for filtering based on a range of values.
133139

134-
QuerySet limitations
135-
~~~~~~~~~~~~~~~~~~~~
140+
``QuerySet`` limitations
141+
~~~~~~~~~~~~~~~~~~~~~~~~
136142

137143
In addition to :ref:`Django MongoDB Backend's QuerySet limitations
138-
<known-issues-limitations-querying>`,
139-
140-
.. TODO
144+
<known-issues-limitations-querying>`, some ``QuerySet`` methods aren't
145+
supported on encrypted fields. Each unsupported method is followed by a sample
146+
error message from the database. Depending on the exact query, error messages
147+
may vary.
148+
149+
- :meth:`~django.db.models.query.QuerySet.order_by`: Cannot add an encrypted
150+
field as a prefix of another encrypted field.
151+
- :meth:`~django.db.models.query.QuerySet.alias`,
152+
:meth:`~django.db.models.query.QuerySet.annotate`,
153+
:meth:`~django.db.models.query.QuerySet.distinct`: Cannot group on field
154+
'_id.value' which is encrypted with the random algorithm or whose encryption
155+
properties are not known until runtime.
156+
- :meth:`~django.db.models.query.QuerySet.dates`,
157+
:meth:`~django.db.models.query.QuerySet.datetimes`: If the value type is a
158+
date, the type of the index must also be date (and vice versa).
159+
- :meth:`~django.db.models.query.QuerySet.in_bulk`: Encrypted fields can't have
160+
unique constraints.
161+
162+
# TODO: add details about joined queries after
163+
https://github.com/mongodb/django-mongodb-backend/pull/443 is finalized.
164+
165+
There are also several ``QuerySet`` methods that aren't permitted on any models
166+
(regardless of whether or not they have encrypted fields) that use a database
167+
connection with Automatic Encryption. Each unsupported method is followed by a
168+
sample error message from the database.
169+
170+
- :meth:`~django.db.models.query.QuerySet.update`: Multi-document updates are
171+
not allowed with Queryable Encryption.
172+
- :meth:`~django.db.models.query.QuerySet.aggregate`,
173+
:meth:`~django.db.models.query.QuerySet.count`: Aggregation stage
174+
$internalFacetTeeConsumer is not allowed or supported with automatic
175+
encryption.
176+
- :meth:`~django.db.models.query.QuerySet.union`: Aggregation stage $unionWith
177+
is not allowed or supported with automatic encryption.

0 commit comments

Comments
 (0)