[Django]-Django: add index to meta field

5👍

Yes you can using model Meta.

if you don’t need to inherit meta from the TimeStampedModel just use this:

class Meta:
   ...

otherwise you need to explicitly tell django to look into the parent’s meta first like this:

class Meta(TimeStampedModel.Meta):

this might be a hackish solution but maybe you can try using index_together to make django create an index for your model:

like this:

class Meta(TimeStampedModel.Meta):
    index_together = [
        ["modified",],
    ]

try it out and tell me if it worked

EDIT:

Another solution coming from: How to override the default value of a Model Field from an Abstract Base Class:

try adding this to your MyModel class

MyModel._meta.get_field('modified').db_index = True

Leave a comment