@@ -10,41 +10,44 @@ The basics
10
10
11
11
Let's consider this example::
12
12
13
- from django_mongodb_backend.fields import EmbeddedModelField
14
- from django_mongodb_backend.models import EmbeddedModel
13
+ from django.db import models
15
14
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
20
17
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
24
32
25
33
26
34
The API is similar to that of Django's relational fields::
27
35
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'
34
41
35
- Represented in BSON, Bob's structure looks like this:
42
+ Represented in BSON, the customer structure looks like this:
36
43
37
44
.. code-block :: js
38
45
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
+ }
48
51
49
52
Querying ``EmbeddedModelField ``
50
53
-------------------------------
0 commit comments