@@ -6,8 +6,13 @@ Use :class:`~django_mongodb_backend.fields.EmbeddedModelField` and
6
6
your data using `embedded documents
7
7
<https://www.mongodb.com/docs/manual/data-modeling/#embedded-data> `_.
8
8
9
+ .. _embeddedmodelfield-example :
10
+
11
+ ``EmbeddedModelField ``
12
+ ----------------------
13
+
9
14
The basics
10
- ----------
15
+ ~~~~~~~~~~
11
16
12
17
Let's consider this example::
13
18
@@ -47,11 +52,61 @@ Represented in BSON, Bob's structure looks like this:
47
52
...
48
53
}
49
54
55
+ Unless explicitly set, embedded models don't have a primary key value.
56
+
50
57
Querying ``EmbeddedModelField ``
51
- -------------------------------
58
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52
59
53
60
You can query into an embedded model using the same double underscore syntax
54
61
as relational fields. For example, to retrieve all customers who have an
55
62
address with the city "New York"::
56
63
57
64
>>> Customer.objects.filter(address__city="New York")
65
+
66
+ .. _embeddedmodelarrayfield-example :
67
+
68
+ ``EmbeddedModelArrayField ``
69
+ ---------------------------
70
+
71
+ The basics
72
+ ~~~~~~~~~~
73
+
74
+ Let's consider this example::
75
+
76
+ from django_mongodb_backend.fields import EmbeddedModelArrayField
77
+ from django_mongodb_backend.models import EmbeddedModel
78
+
79
+ class Customer(models.Model):
80
+ name = models.CharField(...)
81
+ addresses = EmbeddedModelArrayField("Address")
82
+ ...
83
+
84
+ class Address(EmbeddedModel):
85
+ ...
86
+ city = models.CharField(...)
87
+
88
+
89
+ The API is similar to that of Django's relational fields::
90
+
91
+ >>> Customer.objects.create(name="Bob", addresses=[Address(city="New York", ...)], ...)
92
+ >>> bob = Customer.objects.get(name="Bob")
93
+ >>> bob.addresses
94
+ [<Address: Address object>]
95
+ >>> bob.address[0].city
96
+ 'New York'
97
+
98
+ Represented in BSON, Bob's structure looks like this:
99
+
100
+ .. code-block :: js
101
+
102
+ {
103
+ " _id" : ObjectId (... ),
104
+ " name" : " Bob" ,
105
+ " address" : [{
106
+ ...
107
+ " city" : " New York"
108
+ }],
109
+ ...
110
+ }
111
+
112
+ Unless explicitly set, embedded models don't have a primary key value.
0 commit comments