(This is basically a copy of http://stackoverflow.com/q/29680060/1091116 - I hope that this is allowed here)
-- Consider the following `urls.py` - for brevity, I decided to put the example in one file:
from django.conf.urls import patterns, include, url
from django.http import HttpResponse
from django.db import models
def x(request):
class A(models.Model):
title = models.CharField(max_length=1024)
class RightAnchored(models.Lookup):
lookup_name = 'rightanchored'
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 "REVERSE(%s) LIKE REVERSE(%s)" % (lhs, rhs), params
models.CharField.register_lookup(RightAnchored)
q = A.objects.filter(title__rightanchored='%b')
return HttpResponse(str(q.query))
urlpatterns = patterns('',
url(r'^$', x),
)
For some reason, visiting http://localhost:8000 gives me: `SELECT "w_a"."id", "w_a"."title" FROM "w_a" WHERE REVERSE("w_a"."title") LIKE REVERSE(%b)`, while running the command would result in `SELECT "w_a"."id", "w_a"."title" FROM "w_a" WHERE REVERSE("w_a"."title") LIKE REVERSE('%b')` (note the extra `''` inside `LIKE REVERSE`). This means that the displayed query is different than one that is being run. How can I make my Lookup behave properly when `str(q.query)` is called?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/72bdf52f-723e-4c5a-aeb3-f9824f615fb4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment