#29526: Use of QuerySet.extra() when excluding twice a string occurance
--------------------------------+--------------------------------------
Reporter: Manuel Strehl | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 2.0
Severity: Normal | Resolution: invalid
Keywords: QuerySet.extra | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by Simon Charette):
* status: new => closed
* resolution: => invalid
Comment:
Hey Manuel,
I suggest you have a look at the
[https://docs.djangoproject.com/en/2.0/howto/custom-lookups/#a-simple-
lookup-example custom lookup documentation].
You should be able to achieve what you're after by defining a custom
`Like` lookup and registering on `CharField` and `TextField`.
{{{#!python
from django.db import models
class Like(models.Lookup):
lookup_name = 'like'
def as_sql(self, compiler, connection):
lhs, lhs_params = self.process_lhs(compiler, connection)
rhs, rhs_params = self.process_rhs(compiler, connection)
params = lhs_params + rhs_params
return '%s LIKE %s' % (lhs, rhs), params
models.CharField.register_lookup(Like)
models.TextField.register_lookup(Like)
}}}
Then it would be a simple matter of using it in your `exclude()` call
{{{#!python
exclude(name__like='{}%{}'.format(term, term))
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29526#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.d7ffc169f00c593ebcc145a3ac272ac4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.