@@ -10,41 +10,44 @@ The basics
1010
1111Let's consider this example::
1212
13- from django_mongodb_backend.fields import EmbeddedModelField
14- from django_mongodb_backend.models import EmbeddedModel
13+ from django.db import models
1514
16- class Customer(models.Model):
17- name = models.CharField(...)
18- address = EmbeddedModelField("Address")
19- ...
15+ from django_mongodb_backend.fields import EmbeddedModelField
16+ from django_mongodb_backend.models import EmbeddedModel
2017
21- class Address(EmbeddedModel):
22- ...
23- city = models.CharField(...)
18+
19+ class Customer(models.Model):
20+ name = models.CharField(max_length=255)
21+ address = EmbeddedModelField("Address")
22+
23+ def __str__(self):
24+ return self.name
25+
26+
27+ class Address(EmbeddedModel):
28+ city = models.CharField(max_length=255)
29+
30+ def __str__(self):
31+ return self.city
2432
2533
2634The API is similar to that of Django's relational fields::
2735
28- >>> Customer.objects.create(name="Bob", address=Address(city="New York", ...), ...)
29- >>> bob = Customer.objects.get(...)
30- >>> bob.address
31- <Address: Address object>
32- >>> bob.address.city
33- 'New York'
36+ >>> bob = Customer.objects.create(name="Bob", address=Address(city="New York"))
37+ >>> bob.address
38+ <Address: New York>
39+ >>> bob.address.city
40+ 'New York'
3441
35- Represented in BSON, Bob's structure looks like this:
42+ Represented in BSON, the customer structure looks like this:
3643
3744.. code-block :: js
3845
39- {
40- " _id" : ObjectId (... ),
41- " name" : " Bob" ,
42- " address" : {
43- ...
44- " city" : " New York"
45- },
46- ...
47- }
46+ {
47+ _id: ObjectId (' 683df821ec4bbe0692d43388' ),
48+ name: ' Bob' ,
49+ address: { city: ' New York' }
50+ }
4851
4952 Querying ``EmbeddedModelField ``
5053-------------------------------
0 commit comments