pythondjangodjango-modelsdjango-inheritance

Add an index to an inherited field in django


I have many models which need timestamps and cache control and therefore inherit from TimeTrackable from lck.django, a mixin library I am using.

However, for some reason the created and modified fields defined in TimeTrackable are not indexed, even though many other fields in the same mixin library are, and I use sorting based on these fields heavily to varying degrees based on the model and would like to index one or both of them in my postgres database. (I am open to the idea that indexing on these fields is not what I actually need, but I think it's a pretty clear-cut use case for indexes.)

My understanding is that due to quirks in Django's inheritance model, if I just try to override those fields with fields of the same name, plus an index, the system will try to create two identically-named fields and choke on syncdb.

Is there any good way to index them? I can think of a few options:

Are there any other good methods of doing this?


Solution

  • The solution I ended up on was abusing index_together on the child model. Ugly or not, it's the most convenient way to index an individual field (there's no check that index_together tuples have more than one member) and it's declarative and relatively clear to read.