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
0 commit comments