
from django.db import models

class Substance(models.Model):
    name = models.TextField(unique=True, db_index=True)
    alias = models.TextField()

class Station(models.Model):
    name = models.TextField(unique=True, db_index=True)
    alias = models.TextField()

class Unit(models.Model):
    name = models.TextField(unique=True, db_index=True)

class Measurement(models.Model):
    station = models.ForeignKey(Station)
    substance = models.ForeignKey(Substance)
    unit = models.ForeignKey(Unit)
    value = models.FloatField()
    performed = models.DateTimeField()
    written = models.DateTimeField(auto_now=True)


from django.db import models

class Substance(models.Model):
    name = models.TextField(unique=True, db_index=True)
    alias = models.TextField()

class Station(models.Model):
    name = models.TextField(unique=True, db_index=True)
    alias = models.TextField()

class Unit(models.Model):
    name = models.TextField(unique=True, db_index=True)

class Measurement(models.Model):
    station = models.ForeignKey(Station)
    substance = models.ForeignKey(Substance)
    unit = models.ForeignKey(Unit)
    value = models.FloatField()
    performed = models.DateTimeField()
    written = models.DateTimeField(auto_now=True)

    class Meta:
        index_together = [
            ['station', 'substance', 'unit', 'performed']
        ]

Add index for Measurement model
Add index for Measurement model

  
  from django.db import models
  
  class Substance(models.Model):
      name = models.TextField(unique=True, db_index=True)
      alias = models.TextField()
  
  class Station(models.Model):
      name = models.TextField(unique=True, db_index=True)
      alias = models.TextField()
  
  class Unit(models.Model):
      name = models.TextField(unique=True, db_index=True)
  
  class Measurement(models.Model):
      station = models.ForeignKey(Station)
      substance = models.ForeignKey(Substance)
      unit = models.ForeignKey(Unit)
      value = models.FloatField()
      performed = models.DateTimeField()
      written = models.DateTimeField(auto_now=True)
  
+     class Meta:
+         index_together = [
+             ['station', 'substance', 'unit', 'performed']
+         ]
+ 
Add index for Measurement model
## Code Before:

from django.db import models

class Substance(models.Model):
    name = models.TextField(unique=True, db_index=True)
    alias = models.TextField()

class Station(models.Model):
    name = models.TextField(unique=True, db_index=True)
    alias = models.TextField()

class Unit(models.Model):
    name = models.TextField(unique=True, db_index=True)

class Measurement(models.Model):
    station = models.ForeignKey(Station)
    substance = models.ForeignKey(Substance)
    unit = models.ForeignKey(Unit)
    value = models.FloatField()
    performed = models.DateTimeField()
    written = models.DateTimeField(auto_now=True)

## Instruction:
Add index for Measurement model
## Code After:

from django.db import models

class Substance(models.Model):
    name = models.TextField(unique=True, db_index=True)
    alias = models.TextField()

class Station(models.Model):
    name = models.TextField(unique=True, db_index=True)
    alias = models.TextField()

class Unit(models.Model):
    name = models.TextField(unique=True, db_index=True)

class Measurement(models.Model):
    station = models.ForeignKey(Station)
    substance = models.ForeignKey(Substance)
    unit = models.ForeignKey(Unit)
    value = models.FloatField()
    performed = models.DateTimeField()
    written = models.DateTimeField(auto_now=True)

    class Meta:
        index_together = [
            ['station', 'substance', 'unit', 'performed']
        ]
