Python Django Mysql like statement -
I created a query from a dictionary
def search (parameter): Query_data = [] Db_query = "getdata_record_search from SELECT *" where for the key, values in params.iteritems (): query_data.append (key + "like" + value + "% and") db_query = re.sub ("and \ S $ "," ", db_query +" ".join (query_data)) query_data = record_search.objects.raw (db_query) data = serializers.serialize (" json ", query_data) I'm not seeing an expected output when I'm doing this through the manage.py shell, I see the following error.
& gt; & Gt; & Gt; String = 'w3' + '%' & gt; & Gt; & Gt; Db_query = "getData_record_search from SELECT *" where the domain '% s' such as "% string & gt; & Gt; & Gt; Db_query "SELECT * from getdata_record_search where domain 'w3%'" & gt; & Gt; & Gt; Testdata = record_search.objects.raw (db_query)> & gt; & Gt; Testdata & lt; RawQuerySet: "SELECT * FROM getdata_record_search where domain 'w3%'" & gt; & Gt; & Gt; & Gt; Tracebacks (most recent call final): File "console" gt; line 1, & lt; Module & gt; In file = serializers.serialize ("json", testdata) file "/usr/local/lib/python2.6/dist-packages/django/core/serializers/ __init__.py ", line 122, in serialize s.sialialize (queryet, ** option) file" / usr / local / lib / python2.6 / dist-packages / django / core / serializers / base.py ", line 41 , Serialize for obj in query: file "/usr/local/lib/python2.6/dist-packages/django/db/model/currency.py", line 1398, __iter__ query = iter (self.query) file In "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 73, __iter__ self._execute_query () in the file "/ usr / local / lib / python2 .6 / dist-packages / django / db / models / sql / query.py ", line 87, _execute_query, self-cursor .execute (self.sq L, self.params) file "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 69, executed return super (cursor debug rappers, themselves). Execute (SQL, Parameters) file "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 53, returned in self.cursor.execute (sql, paragraph) File "/ usr / local / lib / Python2.6 / dist-packages / django / db / backends / mysql / base.py", line 124, returned the self.cursor.execute (query, args) file "/ usr /lib/pymodules/python2.6/ "MySQLdb / cursors.py", line 151, execute query = query% db.literal (args) in TypeError: there is not enough logic for the format string
What am I doing wrong here?
Checking mysql logs However, I can not see the questions coming out there. However, if I am removing such a statement, and I'm just displaying a You can use a raw query to do anything here It is possible that Django ORM: select from getdata_record, then I can see that the query is actually In mysql the server is logged in.
def search (parameter): filter_kwargs = {'{} __ startwith' .format (key): A Value for the value, value in params.iteritems ()} query_data = record_search.objects.filter (** filter_kwargs) data = serializers.serialize ("json", query_data)
Comments
Post a Comment