Skip to content

DOCSP-46327: Create indexes #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions source/includes/model-data/indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# start-models
from django.db import models
from django.db.models import Q, F
from django_mongodb_backend.models import EmbeddedModel
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField

class Nutrition(EmbeddedModel):
calories = models.IntegerField(default=0)
carb_grams = models.IntegerField(default=0)
protein_grams = models.IntegerField(default=0)

class Recipe(models.Model):
title = models.CharField(max_length=200)
cuisine = models.CharField(max_length=200)
cook_time = models.IntegerField(default=0)
prep_time = models.IntegerField(default=0)
allergens = ArrayField(models.CharField(max_length=100), null=True, blank=True)
nutrition = EmbeddedModelField(Nutrition, null=True, blank=True)

class Meta:
db_table = "recipes"

def __str__(self):
return self.title
# end-models

# start-single-field-meta
class Meta:
db_table = "recipes"
indexes = [
models.Index(fields=["title"], name="title_idx"),
]
# end-single-field-meta

# start-single-field-option
title = models.CharField(max_length=200, db_index=True)
# end-single-field-option

# start-compound
class Meta:
db_table = "recipes"
indexes = [
models.Index(fields=["title", "cook_time"]),
]
# end-compound

# start-multikey
class Meta:
db_table = "recipes"
indexes = [
models.Index(fields=["allergens"], name="allergy_idx"),
]
# end-multikey

# start-embedded
class Meta:
db_table = "recipes"
indexes = [
models.Index(fields=["nutrition"]),
]
# end-embedded

# start-partial
class Meta:
db_table = "recipes"
indexes = [
models.Index(fields=["cuisine"],
condition=Q(cook_time__lt=30),
name="fast_cuisine_idx"),
]
# end-partial

# start-unique-single
cuisine = models.CharField(max_length=200, unique=True)
# end-unique-single

# start-unique-compound
class Meta:
db_table = "recipes"
constraints = [
models.UniqueConstraint(fields=["title", "cuisine"],
name="unique_regional_meal"),
]
# end-unique-compound

# start-expression
class Meta:
db_table = "recipes"
indexes = [
models.Index(expressions=F("cook_time") + F("prep_time"),
name="total_time_idx"),
]
# end-expression
10 changes: 5 additions & 5 deletions source/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Django MongoDB Backend

.. toctree::

Model Your Data </model-data>
Issues & Help </issues-and-help>
Compatibility </compatibility>

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

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

.. Model Your Data
.. ---------------
Model Your Data
---------------

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

.. Django Feature Limitations
.. --------------------------
Expand Down
25 changes: 25 additions & 0 deletions source/model-data.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.. _django-model-data:

===============
Model Your Data
===============

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

.. facet::
:name: genre
:values: reference

.. meta::
:description: Learn how to use Django MongoDB Backend to model MongoDB data.
:keywords: field, query, collection, object

.. toctree::
:titlesonly:
:maxdepth: 1

Create Indexes </model-data/indexes>
Loading
Loading