Skip to content

Commit a1c584a

Browse files
committed
DOCSP-46327: Create indexes
1 parent f8c7bd5 commit a1c584a

File tree

4 files changed

+443
-5
lines changed

4 files changed

+443
-5
lines changed

source/includes/model-data/indexes.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

source/index.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Django MongoDB Backend
1111

1212
.. toctree::
1313

14+
Model Your Data </model-data>
1415
Issues & Help </issues-and-help>
1516
Compatibility </compatibility>
1617

@@ -19,7 +20,6 @@ Django MongoDB Backend
1920
Get Started </get-started>
2021
Connection Configuration </connect>
2122
Interact with Data </interact-data>
22-
Model Your Data </model-data>
2323
Django Feature Limitations </feature-limitations>
2424
API Documentation <{+api+}>
2525

@@ -49,11 +49,11 @@ a Django database backend that uses PyMongo to connect to MongoDB.
4949
.. Learn how to use {+django-odm+} to perform operations on MongoDB data
5050
in the :ref:`django-interact-data` section.
5151

52-
.. Model Your Data
53-
.. ---------------
52+
Model Your Data
53+
---------------
5454

55-
.. Learn how to create Django models that represent MongoDB collections
56-
in the :ref:`django-model-data` section.
55+
Learn how to create Django models that represent MongoDB collections
56+
in the :ref:`django-model-data` section.
5757

5858
.. Django Feature Limitations
5959
.. --------------------------

source/model-data.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.. _django-model-data:
2+
3+
===============
4+
Model Your Data
5+
===============
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:description: Learn how to use Django MongoDB Backend to model MongoDB data.
19+
:keywords: field, query, collection, object
20+
21+
.. toctree::
22+
:titlesonly:
23+
:maxdepth: 1
24+
25+
Create Indexes </model-data/indexes>

0 commit comments

Comments
 (0)