On Wed, Jul 17, 2013 at 8:24 AM, spondbob squelpen <[email protected]> wrote: > I have problem in django using query statement "like %s%" with > Model.objects.raw(). I guess the problem is on '%' character but i don't > know where is it. Here's my model's code: > > def listInRange(self, placeLat, placeLng, typeId, range, placeTag): > tags = '' > if placeTag: > tags = "AND (placeTag LIKE '%" + "%' OR placeTag LIKE > '%".join(placeTag.split(',')) + "%')" > > query = "SELECT *, \ > ( \ > 6371000 * \ > acos( \ > cos( radians(%s) ) * \ > cos( radians(placeLat) ) * \ > cos( radians(placeLng) - radians(%s) ) + \ > sin( radians(%s) ) * \ > sin( radians(placeLat) ) \ > ) \ > ) AS placeDistance, \ > COUNT(r.reviewId) as placeReviews, \ > > (SUM(r.reviewPointPrice)/COUNT(r.reviewId)+SUM(r.reviewPointService)/COUNT(r.reviewId)+SUM(r.reviewPointLocation)/COUNT(r.reviewId)+SUM(r.reviewPointCondition)/COUNT(r.reviewId)+SUM(r.reviewPointComfort)/COUNT(r.reviewId))/5 > AS averagePoint \ > FROM place_place p \ > LEFT JOIN review_review r \ > ON r.reviewPlace_id = p.placeId\ > WHERE placeType_id = %s %s \ > GROUP BY p.placeId \ > HAVING placeDistance < %s \ > ORDER BY placeDistance" > > return Place.objects.raw(query, [placeLat, placeLng, placeLat, > typeId, tags, range]) > > If placeTag is not empty, then it will added to the WHERE condition. When > the placeTag is empty there's no such problem, but when placeTag is not > empty i got this error : > > Warning: Truncated incorrect DOUBLE value: '1AND (placeTag LIKE '%cafe%' > OR placeTag LIKE '%pub%')' > > I guess python make '1 AND (placeTag LIKE '%cafe%' OR placeTag LIKE > '%pub%')' as the value for placeType_id. How can i make the value of > placeType_id=1 and add the AND statement? What's the better way to write the > sql statement using Model.objects.raw() with 'like' statement?
You need '%%' when you want to end up with '%' -- 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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-users. For more options, visit https://groups.google.com/groups/opt_out.

