Skip to content

Instantly share code, notes, and snippets.

@susmithagudapati
Created April 24, 2020 19:05
Show Gist options
  • Save susmithagudapati/0da64fec98828715a7d2e2279c635b49 to your computer and use it in GitHub Desktop.
Save susmithagudapati/0da64fec98828715a7d2e2279c635b49 to your computer and use it in GitHub Desktop.
# create indexes using Field.db_index value
class Product(models.Model):
name = models.CharField(max_length=128, db_index=True)
upc = models.CharField(max_length=32, unique=True)
category = models.ForeignKey(
Category, on_delete=models.PROTECT, related_name='categories', null=True)
created_by = models.ForeignKey(
User, on_delete=models.PROTECT, related_name='product_created_by', null=True)
# create indexes using Meta.indexes option
class Product(models.Model):
name = models.CharField(max_length=128)
upc = models.CharField(max_length=32, unique=True)
category = models.ForeignKey(
Category, on_delete=models.PROTECT, related_name='categories', null=True)
created_by = models.ForeignKey(
User, on_delete=models.PROTECT, related_name='product_created_by', null=True)
class Meta:
indexes = [
models.Index(fields=['name'], name='product_name_idx'),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment