开发者

Error while using date between in django raw query

开发者 https://www.devze.com 2023-03-10 20:46 出处:网络
I am always getting this error TypeError: not all arguments converted during string formatting Here is my query

I am always getting this error TypeError: not all arguments converted during string formatting

Here is my query

State.objects.raw('...review_create_date between %s and %s group by error_type',[fromdate,todate])

Here fromdate=2011-05-21 and todate='2011-05-27'

The above query executes in mysql prompt but could not able to run in python shell

Please some one help me

Here is the traceback

 Traceback (most recent call last):
 File "<console>开发者_高级运维", line 1, in <module>
 File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 1412, in __iter__
 query = iter(self.query)
 File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 73,  in __iter__
 self._execute_query()
 File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 87, in _execute_query
 self.cursor.execute(self.sql, self.params)
 File "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 18, in execute
 sql = self.db.ops.last_executed_query(self.cursor, sql, params)
 File "/usr/local/lib/python2.6/dist-packages/django/db/backends/__init__.py", line 216, in last_executed_query
return smart_unicode(sql) % u_params
TypeError: not all arguments converted during string formatting


django raw sql query in view

I also faced this issue, so I changed the old formatting to new using format and it worked for me.

models.py

class VehicleDamage(models.Model):
    requestdate = models.DateTimeField("requestdate")
    vendor_name = models.CharField("vendor_name", max_length=50)
    class Meta:
        managed=False

views.py

def location_damageReports(request):
    #static date for testing
    date_from = '2019-11-01'
    date_to = '2019-21-01'
    vehicle_damage_reports = VehicleDamage.objects.raw("SELECT * FROM VendorReport_vehicledamage WHERE requestdate BETWEEN '{0}' AND '{1}'".format(date_from, date_to))
    damage_report = DashboardDamageReportSerializer(vehicle_damage_reports, many=True)
    data={"data": damage_report.data}
    return HttpResponse(json.dumps(data), content_type="application/json")


Try using ? instead of %s.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号