Skip to content

Commit e8b7ac1

Browse files
committed
Document Query types vs. Django lookups
1 parent d708364 commit e8b7ac1

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

docs/topics/queryable-encryption.rst

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,8 @@ Once you have defined your models, create the migrations with ``python manage.py
6060
makemigrations`` and run the migrations with ``python manage.py migrate``. Then
6161
create and manipulate instances of the data just like any other Django model
6262
data. The fields will automatically handle encryption and decryption, ensuring
63-
that sensitive data is stored securely in the database.
64-
65-
.. TODO
66-
67-
.. code-block:: console
68-
69-
$ python manage.py shell
70-
>>> from myapp.models import Patient, PatientRecord, Billing
71-
>>> billing = Billing(cc_type="Visa", cc_number="4111111111111111")
72-
>>> patient_record = PatientRecord(ssn="123-45-6789", billing=billing)
73-
>>> patient = Patient.objects.create(
74-
patient_name="John Doe",
75-
patient_id=123456789,
76-
patient_record=patient_record,
77-
)
78-
79-
.. code-block:: console
80-
81-
>>> john = Patient.objects.get(name="John Doe")
82-
>>> john.patient_record.ssn
83-
'123-45-6789'
84-
85-
.. code-block:: console
86-
87-
>>> john.patient_record.ssn
88-
Binary(b'\x0e\x97sv\xecY\x19Jp\x81\xf1\\\x9cz\t1\r\x02...', 6)
63+
that :ref:`sensitive data is stored securely in the database
64+
<manual:qe-features-encryption-at-rest>`.
8965

9066
Querying encrypted fields
9167
-------------------------
@@ -101,6 +77,14 @@ query type in the model field definition. For example, if you want to query the
10177
billing = EncryptedEmbeddedModelField("Billing")
10278
bill_amount = models.DecimalField(max_digits=10, decimal_places=2)
10379
80+
Then you can perform a query like this:
81+
82+
.. code-block:: console
83+
84+
>>> patient = Patient.objects.get(patient_record__ssn="123-45-6789")
85+
>>> patient.name
86+
'John Doe'
87+
10488
.. _qe-available-query-types:
10589

10690
Available query types
@@ -116,31 +100,29 @@ that can be performed on the field. The :ref:`available query types
116100
You can configure an encrypted field for either equality or range queries, but
117101
not both.
118102

119-
Now you can perform queries on the ``ssn`` field using the defined query type.
120-
For example, to find a patient by their SSN, you can do the following::
103+
.. admonition:: Query types vs. Django lookups
121104

122-
from myapp.models import Patient
105+
Range queries in Queryable Encryption are different from Django's
106+
:ref:`range lookups <django:field-lookups>`. Range queries allow you to
107+
perform comparisons on encrypted fields, while Django's range lookups are
108+
used for filtering based on a range of values.
123109

124-
>>> patient = Patient.objects.get(patient_record__ssn="123-45-6789")
125-
>>> patient.name
126-
'John Doe'
110+
If you have an encrypted field that supports range queries like this:
127111

128-
.. admonition:: Range queries vs. lookups
112+
.. code-block:: python
129113
130-
Range queries in Queryable Encryption are different from Django's
131-
:ref:`range lookups <django:field-lookups>`
132-
Range queries allow you to perform comparisons on encrypted fields,
133-
while Django's range lookups are used for filtering based on a range of
134-
values.
114+
class PatientRecord(EmbeddedModel):
115+
ssn = EncryptedCharField(max_length=11, queries={"queryType": "range"})
116+
billing = EncryptedEmbeddedModelField("Billing")
117+
bill_amount = models.DecimalField(max_digits=10, decimal_places=2)
135118
136-
For example, if you have an encrypted field that supports range queries, you
137-
can perform a query like this::
119+
You can perform a query like this:
138120

139-
from myapp.models import Patient
121+
.. code-block:: console
140122
141-
>>> patients = Patient.objects.filter(patient_record__ssn__gte="123-45-0000",
142-
... patient_record__ssn__lte="123-45-9999")
123+
>>> patients = Patient.objects.filter(patient_record__ssn__gte="123-45-0000",
124+
... patient_record__ssn__lte="123-45-9999")
143125
144-
This will return all patients whose SSN falls within the specified range.
126+
This will return all patients whose SSN falls within the specified range.
145127

146128
.. _Python Queryable Encryption Tutorial: https://github.com/mongodb/docs/tree/main/content/manual/manual/source/includes/qe-tutorials/python

0 commit comments

Comments
 (0)