Skip to content

Commit 3e4fd5c

Browse files
authored
DOCSP-46327: Create indexes (#8)
* DOCSP-46327: Create indexes * edits * fixes * clarify * remove expression indexes * embeddedmodel * embedded doc note * MM feedback
1 parent 5765e26 commit 3e4fd5c

File tree

3 files changed

+388
-3
lines changed

3 files changed

+388
-3
lines changed

source/includes/model-data/indexes.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# start-models
2+
from django.db import models
3+
from django.db.models import Q, F
4+
from django_mongodb_backend.models import EmbeddedModel
5+
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField
6+
7+
class Nutrition(EmbeddedModel):
8+
calories = models.IntegerField(default=0)
9+
carb_grams = models.IntegerField(default=0)
10+
protein_grams = models.IntegerField(default=0)
11+
12+
class Recipe(models.Model):
13+
title = models.CharField(max_length=200)
14+
cuisine = models.CharField(max_length=200)
15+
cook_time = models.IntegerField(default=0)
16+
allergens = ArrayField(models.CharField(max_length=100), null=True, blank=True)
17+
nutrition = EmbeddedModelField(Nutrition, null=True, blank=True)
18+
19+
class Meta:
20+
db_table = "recipes"
21+
22+
def __str__(self):
23+
return self.title
24+
# end-models
25+
26+
# start-single-field-meta
27+
class Meta:
28+
db_table = "recipes"
29+
indexes = [
30+
models.Index(fields=["title"], name="title_idx"),
31+
]
32+
# end-single-field-meta
33+
34+
# start-single-field-option
35+
class Recipe(models.Model):
36+
title = models.CharField(max_length=200, db_index=True)
37+
# end-single-field-option
38+
39+
# start-compound
40+
class Meta:
41+
db_table = "recipes"
42+
indexes = [
43+
models.Index(fields=["title", "cook_time"]),
44+
]
45+
# end-compound
46+
47+
# start-multikey
48+
class Meta:
49+
db_table = "recipes"
50+
indexes = [
51+
models.Index(fields=["allergens"], name="allergy_idx"),
52+
]
53+
# end-multikey
54+
55+
# start-embedded
56+
class Meta:
57+
db_table = "recipes"
58+
indexes = [
59+
models.Index(fields=["nutrition"]),
60+
]
61+
# end-embedded
62+
63+
# start-partial
64+
class Meta:
65+
db_table = "recipes"
66+
indexes = [
67+
models.Index(fields=["cuisine"],
68+
condition=Q(cook_time__lt=30),
69+
name="fast_cuisine_idx"),
70+
]
71+
# end-partial
72+
73+
# start-unique-single
74+
cuisine = models.CharField(max_length=200, unique=True)
75+
# end-unique-single
76+
77+
# start-unique-compound
78+
class Meta:
79+
db_table = "recipes"
80+
constraints = [
81+
models.UniqueConstraint(fields=["title", "cuisine"],
82+
name="unique_regional_meal"),
83+
]
84+
# end-unique-compound

source/model-data.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ Model Your Data
2222
:titlesonly:
2323
:maxdepth: 1
2424

25+
Create Indexes </model-data/indexes>
2526
Create Models </model-data/models>
26-
27-
.. TODO:
28-
Create Indexes </model-data/models>
2927

3028
In this section, you can learn how to create and customize
3129
your Django models for use with MongoDB.

0 commit comments

Comments
 (0)