-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
Embedded models | ||
=============== | ||
|
||
Use :class:`~django_mongdob.fields.EmbeddedModelField` to structure your data | ||
using `embedded documents | ||
<https://www.mongodb.com/docs/manual/data-modeling/#embedded-data>`_. | ||
|
||
The basics | ||
---------- | ||
|
||
Let's consider this example:: | ||
|
||
from django_mongodb_backend.fields import EmbeddedModelField | ||
|
||
class Customer(models.Model): | ||
name = models.CharField(...) | ||
address = EmbeddedModelField("Address") | ||
... | ||
|
||
class Address(models.Model): | ||
... | ||
city = models.CharField(...) | ||
|
||
|
||
The API is similar to that of Django's relational fields:: | ||
|
||
>>> Customer.objects.create(name="Bob", address=Address(city="New York", ...), ...) | ||
>>> bob = Customer.objects.get(...) | ||
>>> bob.address | ||
<Address: Address object> | ||
>>> bob.address.city | ||
'New York' | ||
|
||
Represented in BSON, Bob's structure looks like this: | ||
|
||
.. code-block:: js | ||
{ | ||
"_id": ObjectId(...), | ||
"name": "Bob", | ||
"address": { | ||
... | ||
"city": "New York" | ||
}, | ||
... | ||
} | ||
Querying ``EmbeddedModelField`` | ||
------------------------------- | ||
|
||
You can query into an embedded model using the same double underscore syntax | ||
as relational fields. For example, to retrieve all customers who have an | ||
address with the city "New York":: | ||
|
||
>>> Customer.objects.filter(address__city="New York") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters