@@ -18,35 +18,46 @@ your sensitive data.
18
18
The basics
19
19
----------
20
20
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:
23
22
24
23
.. code-block :: python
25
24
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} ) "
28
32
29
33
30
- class Patient (models .Model ):
31
- name = models.CharField(max_length = 255 )
34
+ class PatientRecord (EmbeddedModel ):
32
35
ssn = EncryptedCharField(max_length = 11 )
36
+ billing = EncryptedEmbeddedModelField(" Billing" )
37
+ bill_amount = models.DecimalField(max_digits = 10 , decimal_places = 2 )
33
38
34
- def __str__ (self ):
35
- return self .name
36
39
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
39
47
create and manipulate instances of the data just like any other Django model
40
48
data. The fields will automatically handle encryption and decryption, ensuring
41
49
that sensitive data is stored securely in the database.
42
50
43
- From an encrypted client, you can access the data::
51
+ From an encrypted client, enter the secure data::
44
52
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
+ )
50
61
51
62
From an unencrypted client, you can still access the data, but the sensitive
52
63
fields 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::
55
66
from myapp.models import Patient
56
67
57
68
>>> bob = Patient.objects.get(name="Bob")
58
- >>> bob.ssn
69
+ >>> bob.patient_record. ssn
59
70
Binary(b'\x0e\x97sv\xecY\x19Jp\x81\xf1\\\x9cz\t1\r\x02...', 6)
60
71
61
72
Querying encrypted fields
@@ -67,16 +78,10 @@ query type in the model field definition. For example, if you want to query the
67
78
68
79
.. code-block :: python
69
80
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 ):
76
82
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 )
80
85
81
86
Query types
82
87
~~~~~~~~~~~
@@ -96,7 +101,7 @@ For example, to find a patient by their SSN, you can do the following::
96
101
97
102
from myapp.models import Patient
98
103
99
- >>> patient = Patient.objects.get(ssn ="123-45-6789")
104
+ >>> patient = Patient.objects.get(patient_record__ssn ="123-45-6789")
100
105
>>> patient.name
101
106
'Bob'
102
107
0 commit comments