nshafer / django-hashid-field

Django Model Field that uses Hashids to obscure the value
MIT License
370 stars 40 forks source link

Add real contains/icontains lookup #83

Closed chunkai-meng closed 1 year ago

chunkai-meng commented 1 year ago

All lookup are exact look, would you please support the contains lookup? thanks!

class HashidFieldMixin(object):
    ....
    exact_lookups = ('exact', 'iexact', 'contains', 'icontains')`

```py
    def get_lookup(self, lookup_name):
        ....
        if lookup_name in self.exact_lookups:
            return HashidExactLookup
nshafer commented 1 year ago

Sorry it's not possible with the way that DHF is implemented. The field is stored as an integer, so to do a proper contains query we would need to load every single id in the database, run them through hashids, then search in memory. You will need to store the hashid yourself in a separate field, then you can do contains queries on that field. But that will still require the database to do a full table scan unless you only do startswith.