I am trying to do this in Django:
ThatModel.objects.filter(desired_moisture__lte=Q(
F("sensor__moisture") + F("sensor__calibrate_low"))
/ F("sensor__calibrate_high") + F("upper_deviation"))
This is kind of a codeful, so here's what I'm trying to do in a less-wordy way:
a <= (b + c) / d + e
However, this results in this:
[2014-06-10 00:17:22,724: ERROR/MainProcess] Task pidrator.hardware_controller.CheckIrrigators[67c82981-780e-4c39-b88c-08a728773dd1] raised unexpected: IntegrityError("Failed processing format-parameters; Python 'q' cannot be converted to a MySQL type",)
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/lib/python3.4/site-packages/celery/app/trace.py", line 437, in __protected_call__
return self.run(*args, **kwargs)
File "/home/max/programming/pidrator/pidrator/hardware_controller.py", line 93, in CheckIrrigators
**filter_args):
File "/usr/lib/python3.4/site-packages/django/db/models/query.py", line 96, in __iter__
self._fetch_all()
File "/usr/lib/python3.4/site-packages/django/db/models/query.py", line 857, in _fetch_all
self._result_cache = list(self.iterator())
File "/usr/lib/python3.4/site-packages/django/db/models/query.py", line 220, in iterator
for row in compiler.results_iter():
File "/usr/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 713, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python3.4/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python3.4/site-packages/mysql/connector/django/base.py", line 117, in execute
return self._execute_wrapper(self.cursor.execute, query, args)
File "/usr/lib/python3.4/site-packages/mysql/connector/django/base.py", line 103, in _execute_wrapper
utils.IntegrityError(err.msg), sys.exc_info()[2])
File "/usr/lib/python3.4/site-packages/django/utils/six.py", line 549, in reraise
raise value.with_traceback(tb)
File "/usr/lib/python3.4/site-packages/mysql/connector/django/base.py", line 99, in _execute_wrapper
return method(query, args)
File "/usr/lib/python3.4/site-packages/mysql/connector/cursor.py", line 502, in execute
psub = _ParamSubstitutor(self._process_params(params))
File "/usr/lib/python3.4/site-packages/mysql/connector/cursor.py", line 378, in _process_params
"Failed processing format-parameters; %s" % err)
django.db.utils.IntegrityError: Failed processing format-parameters; Python 'q' cannot be converted to a MySQL type
desired_moisture__lte seems to be unable to handle the Q object I'm passing to it. How do I make this work?
Get rid of the wrapping Q()
and you should have a working result. See https://docs.djangoproject.com/en/dev/topics/db/queries/#using-f-expressions-in-filters for examples.
ThatModel.objects.filter(desired_moisture__lte=(F("sensor__moisture") + F("sensor__calibrate_low")) / F("sensor__calibrate_high") + F("upper_deviation"))