@@ -6,8 +6,13 @@ Use :class:`~django_mongodb_backend.fields.EmbeddedModelField` and
66your data using `embedded documents
77<https://www.mongodb.com/docs/manual/data-modeling/#embedded-data> `_.
88
9+ .. _embeddedmodelfield-example :
10+
11+ ``EmbeddedModelField ``
12+ ----------------------
13+
914The basics
10- ----------
15+ ~~~~~~~~~~
1116
1217Let's consider this example::
1318
@@ -47,11 +52,61 @@ Represented in BSON, Bob's structure looks like this:
4752 ...
4853 }
4954
55+ Unless explicitly set, embedded models don't have a primary key value.
56+
5057Querying ``EmbeddedModelField ``
51- -------------------------------
58+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5259
5360You can query into an embedded model using the same double underscore syntax
5461as relational fields. For example, to retrieve all customers who have an
5562address with the city "New York"::
5663
5764 >>> 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