Rodwan Bakkar
Rodwan Bakkar

Reputation: 484

Filtering django queryset by text applying the diacritics

I am trying to filter a queryset in python by a text

the model is:

models.Offer
       id = pk           
       description = text

I am trying to filter it like:

someText = self.shave_marks(someText)
offers = offers.filter(description__icontains=someText)

Where the shave_marks is replacing the special characters like: ç will become c.

The text in the database (in the description field) also has special characters, what I need is to "shave" the description text first then do the filtering.

Any help, thank you very much!!!

Upvotes: 0

Views: 585

Answers (2)

Ojas Kale
Ojas Kale

Reputation: 2159

How about this ?

offers = [(x, x.description)) for x in offers.objects.all()]

required_offers = []

for key, value in offers:
    if someText in shave_marks(value):
        required_offers.append(key)

Upvotes: 1

Ojas Kale
Ojas Kale

Reputation: 2159

what you can do is, create a custom field extending charfield. have that field override method get_prep_value

I could not find a concrete example, but in theory this should work.

class SpecialField(models.CharField):
    def get_prep_value(self, value):
        return shave_marks(shave_marks)

Upvotes: 0

Related Questions