22Encrypted fields
33================
44
5- .. versionadded :: 5.2.3
6-
7- Django MongoDB Backend supports :doc: `manual:core/queryable-encryption `.
5+ .. currentmodule :: django_mongodb_backend.fields
86
9- See :doc: `/howto/queryable-encryption ` for more information on how to use
10- Queryable Encryption with Django MongoDB Backend.
7+ .. versionadded :: 5.2.3
118
12- See the :doc: `/topics/queryable-encryption ` topic guide for
13- more information on developing applications with Queryable Encryption .
9+ To use encrypted fields, you must :doc: `configure Queryable Encryption
10+ </howto/queryable-encryption>` .
1411
1512The following tables detailed which fields have encrypted counterparts. In all
1613cases, the encrypted field names are simply prefixed with ``Encrypted ``, e.g.
@@ -48,19 +45,62 @@ cases, the encrypted field names are simply prefixed with ``Encrypted``, e.g.
4845.. csv-table :: ``django_mongodb_backend.fields``
4946 :header: "Model Field", "Encrypted version available?"
5047
51- :class: `~.fields.ArrayField `, Yes
52- :class: `~.fields.EmbeddedModelArrayField `, Yes
53- :class: `~.fields.EmbeddedModelField `, Yes
54- :class: `~.fields.ObjectIdField `, Yes
55- :class: `~.fields.PolymorphicEmbeddedModelField `, No: may be implemented in the future.
56- :class: `~.fields.PolymorphicEmbeddedModelArrayField `, No: may be implemented in the future.
48+ :class: `ArrayField `, Yes
49+ :class: `EmbeddedModelArrayField `, Yes
50+ :class: `EmbeddedModelField `, Yes
51+ :class: `ObjectIdField `, Yes
52+ :class: `PolymorphicEmbeddedModelField `, No: may be implemented in the future.
53+ :class: `PolymorphicEmbeddedModelArrayField `, No: may be implemented in the future.
54+
55+ .. _encrypted-fields-queries :
56+
57+ ``EncryptedField.queries ``
58+ --------------------------
59+
60+ Most encrypted fields* take an optional ``queries `` argument. It's a dictionary
61+ that specifies the type of queries that can be performed on the field, as well
62+ as any query options.
5763
58- These fields don't support the ``queries `` argument:
64+ The :ref: `available query types <manual:qe-fundamentals-encrypt-query >` depend
65+ on your version of MongoDB. For example, in MongoDB 8.0, the supported types
66+ are ``equality `` and ``range ``.
67+
68+ .. admonition :: Query types vs. Django lookups
69+
70+ Range queries in Queryable Encryption are different from Django's
71+ :ref: `range lookups <django:field-lookups >`. Range queries allow you to
72+ perform comparisons on encrypted fields, while Django's range lookups are
73+ used for filtering based on a range of values.
74+
75+ \* These fields don't support the ``queries `` argument:
5976
6077- ``EncryptedArrayField ``
6178- ``EncryptedEmbeddedModelArrayField ``
6279- ``EncryptedEmbeddedModelField ``
6380
81+ Embedded model encryption
82+ =========================
83+
84+ There are two ways to encrypt embedded models. You can either encrypt the
85+ entire subdocument, in which case you can't query any the subdocuments fields,
86+ or you can encrypt only selected fields of the subdocument.
87+
88+ Encrypting the entire subdocument
89+ ---------------------------------
90+
91+ To encrypt a subdocument, use ``EncryptedEmbeddedModelField `` or
92+ ``EncryptedEmbeddedModelArrayField ``. In this case, the field's embedded model
93+ cannot have any encrypted fields.
94+
95+ Encrypting selected fields of a subdocument
96+ -------------------------------------------
97+
98+ To encrypt only select fields of a subdocument, use :class: `EmbeddedModelField `
99+ and any of the other encrypted fields on the embedded model.
100+
101+ MongoDB doesn't support encrypting selected fields of
102+ ``EmbeddedModelArrayField ``.
103+
64104Limitations
65105===========
66106
@@ -70,20 +110,54 @@ MongoDB imposes some restrictions on encrypted fields:
70110* They cannot be part of a unique constraint.
71111* They cannot be null.
72112
113+ ``QuerySet `` limitations
114+ ------------------------
115+
116+ In addition to :ref: `Django MongoDB Backend's QuerySet limitations
117+ <known-issues-limitations-querying>`, some ``QuerySet `` methods aren't
118+ supported on encrypted fields. Each unsupported method is followed by a sample
119+ error message from the database. Depending on the exact query, error messages
120+ may vary.
121+
122+ - :meth: `~django.db.models.query.QuerySet.order_by `: Cannot add an encrypted
123+ field as a prefix of another encrypted field.
124+ - :meth: `~django.db.models.query.QuerySet.alias `,
125+ :meth: `~django.db.models.query.QuerySet.annotate `,
126+ :meth: `~django.db.models.query.QuerySet.distinct `: Cannot group on field
127+ '_id.value' which is encrypted with the random algorithm or whose encryption
128+ properties are not known until runtime.
129+ - :meth: `~django.db.models.query.QuerySet.dates `,
130+ :meth: `~django.db.models.query.QuerySet.datetimes `: If the value type is a
131+ date, the type of the index must also be date (and vice versa).
132+ - :meth: `~django.db.models.query.QuerySet.in_bulk `: Encrypted fields can't have
133+ unique constraints.
134+
135+ # TODO: add details about joined queries after
136+ https://github.com/mongodb/django-mongodb-backend/pull/443 is finalized.
137+
138+ There are also several ``QuerySet `` methods that aren't permitted on any models
139+ (regardless of whether or not they have encrypted fields) that use a database
140+ connection with Automatic Encryption. Each unsupported method is followed by a
141+ sample error message from the database.
142+
143+ - :meth: `~django.db.models.query.QuerySet.update `: Multi-document updates are
144+ not allowed with Queryable Encryption.
145+ - :meth: `~django.db.models.query.QuerySet.aggregate `,
146+ :meth: `~django.db.models.query.QuerySet.count `: Aggregation stage
147+ $internalFacetTeeConsumer is not allowed or supported with automatic
148+ encryption.
149+ - :meth: `~django.db.models.query.QuerySet.union `: Aggregation stage $unionWith
150+ is not allowed or supported with automatic encryption.
151+
73152``EncryptedFieldMixin ``
74153=======================
75154
76155.. class :: EncryptedFieldMixin
77156
78157 .. versionadded :: 5.2.3
79158
80- A mixin that can be used to create custom encrypted fields with Queryable
81- Encryption.
82-
83- To create an encrypted field, inherit from ``EncryptedFieldMixin `` and
84- your custom field class:
85-
86- .. code-block :: python
159+ Use this mixin to create encrypted versions of your own custom fields. For
160+ example, to create an encrypted version of ``MyField ``::
87161
88162 from django.db import models
89163 from django_mongodb_backend.fields import EncryptedFieldMixin
@@ -93,16 +167,5 @@ MongoDB imposes some restrictions on encrypted fields:
93167 class MyEncryptedField(EncryptedFieldMixin, MyField):
94168 pass
95169
96-
97- You can then use your custom encrypted field in a model, specifying the
98- desired query types:
99-
100- .. code-block :: python
101-
102- class MyModel (models .Model ):
103- my_encrypted_field = MyEncryptedField(
104- queries = {" queryType" : " equality" },
105- )
106- my_encrypted_field_too = MyEncryptedField(
107- queries = {" queryType" : " range" },
108- )
170+ This adds the :ref: `queries <encrypted-fields-queries >` argument to the
171+ field.
0 commit comments