@@ -18,35 +18,46 @@ your sensitive data.
1818The basics
1919----------
2020
21- For example, you can define a model with encrypted fields
22- like this:
21+ For example, you can define models with encrypted fields like this:
2322
2423.. code-block :: python
2524
26- from django.db import models
27- from django_mongodb_backend.fields import EncryptedCharField
25+ class Patient (EncryptedTestModel ):
26+ patient_name = models.CharField(max_length = 255 )
27+ patient_id = models.BigIntegerField()
28+ patient_record = EmbeddedModelField(" PatientRecord" )
29+
30+ def __str__ (self ):
31+ return f " { self .patient_name} ( { self .patient_id} ) "
2832
2933
30- class Patient (models .Model ):
31- name = models.CharField(max_length = 255 )
34+ class PatientRecord (EmbeddedModel ):
3235 ssn = EncryptedCharField(max_length = 11 )
36+ billing = EncryptedEmbeddedModelField(" Billing" )
37+ bill_amount = models.DecimalField(max_digits = 10 , decimal_places = 2 )
3338
34- def __str__ (self ):
35- return self .name
3639
37- Once you have defined your model, created migrations with ``python manage.py
38- makemigrations `` and run migrations with ``python manage.py migrate ``, you can
40+ class Billing (EmbeddedModel ):
41+ cc_type = models.CharField(max_length = 50 )
42+ cc_number = models.CharField(max_length = 20 )
43+
44+
45+ Once you have defined your models, create the migrations with ``python manage.py
46+ makemigrations `` and run the migrations with ``python manage.py migrate ``. Then
3947create and manipulate instances of the data just like any other Django model
4048data. The fields will automatically handle encryption and decryption, ensuring
4149that sensitive data is stored securely in the database.
4250
43- From an encrypted client, you can access the data::
51+ From an encrypted client, enter the secure data::
4452
45- from myapp.models import Patient
46-
47- >>> bob = Patient.objects.create(name="Bob", ssn="123-45-6789")
48- >>> bob.ssn
49- '123-45-6789'
53+ >>> from myapp.models import Patient, PatientRecord, Billing
54+ >>> billing = Billing(cc_type="Visa", cc_number="4111111111111111")
55+ >>> patient_record = PatientRecord(ssn="123-45-6789", billing=self.billing)
56+ >>> patient = Patient.objects.create(
57+ patient_name="John Doe",
58+ patient_id=123456789,
59+ patient_record=self.patient_record,
60+ )
5061
5162From an unencrypted client, you can still access the data, but the sensitive
5263fields will be encrypted. For example, if you try to access the ``ssn `` field
@@ -55,7 +66,7 @@ from an unencrypted client, you will see the encrypted value::
5566 from myapp.models import Patient
5667
5768 >>> bob = Patient.objects.get(name="Bob")
58- >>> bob.ssn
69+ >>> bob.patient_record. ssn
5970 Binary(b'\x0e\x97sv\xecY\x19Jp\x81\xf1\\\x9cz\t1\r\x02...', 6)
6071
6172Querying encrypted fields
@@ -67,16 +78,10 @@ query type in the model field definition. For example, if you want to query the
6778
6879.. code-block :: python
6980
70- from django.db import models
71- from django_mongodb_backend.fields import EncryptedCharField
72-
73-
74- class Patient (models .Model ):
75- name = models.CharField(max_length = 255 )
81+ class PatientRecord (EmbeddedModel ):
7682 ssn = EncryptedCharField(max_length = 11 , queries = {" queryType" : " equality" })
77-
78- def __str__ (self ):
79- return self .name
83+ billing = EncryptedEmbeddedModelField(" Billing" )
84+ bill_amount = models.DecimalField(max_digits = 10 , decimal_places = 2 )
8085
8186 Query types
8287~~~~~~~~~~~
@@ -96,7 +101,7 @@ For example, to find a patient by their SSN, you can do the following::
96101
97102 from myapp.models import Patient
98103
99- >>> patient = Patient.objects.get(ssn ="123-45-6789")
104+ >>> patient = Patient.objects.get(patient_record__ssn ="123-45-6789")
100105 >>> patient.name
101106 'Bob'
102107
0 commit comments