I have a model class which has a comma separated integer field which maps to a Postgres integer array field.
class Observation(models.Model):
frequencies = models.CommaSeparatedIntegerField(max_length=200)
What I want to be able to do is use the filter (or something else suitable) interface to do a comparison on an indexed value in the array.
Example:
Observation.objects.filter(frequencies[100]__equals = 10)
I know I can not do this and I can not see in the documentation how to index an array field in the filter interface. I want this action to be performed by the SQL in the database (may have to do it in code?).
I know I can use the extra() feature but I want to know if there is a better way?
Thanks.
If you find yourself needing to search particular entities within a comma-separated field, you should probably consider whether that’s the correct format for your data. Perhaps a foreign key to a separate table might be more appropriate.