[英]SQL function in ORM
I have SQL Server, FreeTDS, Django 1.5.5. 我有SQL Server,FreeTDS,Django 1.5.5。 For SQL Server's full text search, I need to use contains function of Sql server.
对于SQL Server的全文本搜索,我需要使用Sql Server的contains函数。
This will search "mytext" in my_table's x_name field. 这将在my_table的x_name字段中搜索“ mytext”。
select x_name from my_table where contains(x_name, 'mytext')
For doing it with ORM, there is djorm-ext-expressions. 为了使用ORM进行操作,有djorm-ext-expressions。
http://www.niwi.be/2012/10/07/sqlexpressions-and-functions-with-django/ http://www.niwi.be/2012/10/07/sqlexpressions-and-functions-with-django/
That works fine if there is only one argument for the sql function, but contains have two: field name and text. 如果sql函数只有一个参数,但包含两个参数,则该方法很好:字段名和文本。
It generates Sql's Where statement like this: 它生成Sql的Where语句,如下所示:
MyTable.objects.where(Contains("x_name", "mytext"))
.query.get_compiler("default").as_sql()
Output: 输出:
WHERE (contains("x_name"))', ('mytext',))
But it should output like this: 但是它应该输出如下:
WHERE (contains("x_name", 'mytext'))
Anyone knows how to fix it? 有人知道如何解决吗?
我已经解决了这个问题:
MyTable.objects.where(RawExpression("contains(x_name, %s)", 'mytext'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.