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:
- copy/paste the code of TimeTrackable into my app (not DRY but at least the consequences are predictable)
- add a separate index step in a south migration or syncdb hook or something (seems fragile, and confusing for someone trying to tell what columns are indexed by looking at the application code, given some columns are indeed explicitly indexed in the model files)
- index it manually in postgres after the application is initialized (I have to hand this application off to a client, so I would like to minimize manual deployment steps)
- somehow use/abuse the "index together" meta field to index individual fields instead of its intended use (no idea what the consequences of this would be, were I to try it)
Are there any other good methods of doing this?
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.
For those who are looking at solutions for django<1.5 use uniqe_together