1+ # start-models
2+ from django .db import models
3+ from django .db .models import Q , F
4+ from django_mongodb_backend .fields import EmbeddedModelField , ArrayField
5+
6+ class Nutrition (models .Model ):
7+ calories = models .IntegerField (default = 0 )
8+ fat_grams = 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+ prep_time = models .IntegerField (default = 0 )
17+ allergens = ArrayField (models .CharField (max_length = 100 ), null = True , blank = True )
18+ nutrition = EmbeddedModelField (Nutrition , null = True , blank = True )
19+
20+ class Meta :
21+ db_table = "recipes"
22+
23+ def __str__ (self ):
24+ return self .title
25+ # end-models
26+
27+ # start-single-field-meta
28+ class Meta :
29+ db_table = "recipes"
30+ indexes = [
31+ models .Index (fields = ["title" ], name = "title_idx" ),
32+ ]
33+ # end-single-field-meta
34+
35+ # start-single-field-option
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+ title = models .CharField (max_length = 200 , unique = True )
75+ # end-unique-single
76+
77+ # start-unique-compound
78+ class Meta :
79+ db_table = "recipes"
80+ indexes = [
81+ models .Index (fields = ["title" , "cuisine" ]),
82+ ]
83+ constraints = [
84+ models .UniqueConstraint (fields = ["title" , "cuisine" ], name = "unique_regional_meal" )
85+ ]
86+ # end-unique-compound
87+
88+ # start-multikey
89+ class Meta :
90+ db_table = "recipes"
91+ indexes = [
92+ models .Index (F ("cook_time" ) + F ("prep_time" ), name = "total_time_idx" ),
93+ ]
94+ # end-multikey
0 commit comments