数据库功能的转换结果不起作用

时间:2018-02-27 10:20:50

标签: mysql django django-queryset django-database

我使用以下内容获取ISO周日期:

class DateWeek(Func):
  """ Return the ISO8601 Week Number """

  def as_mysql(self, compiler, connection):
    self.function = 'WEEK'
    self.output_field = IntegerField()
    self.template = 'CONVERT(%(function)s(%(expressions)s, 3), UNSIGNED INTEGER)'
    return super().as_sql(compiler, connection)

哪种方法正常:

MyObj.annotate(wk=DateWeek('date')).values('wk')

<QuerySet [{'wk': 1}, {'wk': 21}]>

......直到我想做类似的事情:

MyObj.annotate(wk=DateWeek('date')).filter(wk__gt=20)

当我尝试这样做时,我得到一个dateparse错误,因为Django正在尝试计算过滤器,假设wk是某种日期。我已经将output_field设置为IntegerField,还有什么我应该做的吗?

-----------------------------------------------------
TypeError           Traceback (most recent call last)
<ipython-input-8-16eccbbc84a8> in <module>()
----> 9       ).filter(dy__gt=2016).values('dw', 'dy')

~/.virtualenvs/f4c2/lib/python3.6/site-packages/django/db/models/query.py in filter(self, *args, **kwargs)
    834         set.
    835         """
--> 836         return self._filter_or_exclude(False, *args, **kwargs)
    837 
    838     def exclude(self, *args, **kwargs):

~/.virtualenvs/f4c2/lib/python3.6/site-packages/django/db/models/query.py in _filter_or_exclude(self, negate, *args, **kwargs)
    852             clone.query.add_q(~Q(*args, **kwargs))
    853         else:
--> 854             clone.query.add_q(Q(*args, **kwargs))
    855         return clone
    856 

~/.virtualenvs/f4c2/lib/python3.6/site-packages/django/db/models/sql/query.py in add_q(self, q_object)
   1250         # So, demotion is OK.
   1251         existing_inner = {a for a in self.alias_map if self.alias_map[a].join_type == INNER}
-> 1252         clause, _ = self._add_q(q_object, self.used_aliases)
   1253         if clause:
   1254             self.where.add(clause, AND)

~/.virtualenvs/f4c2/lib/python3.6/site-packages/django/db/models/sql/query.py in _add_q(self, q_object, used_aliases, branch_negated, current_negated, allow_joins, split_subq)
   1274                     child, can_reuse=used_aliases, branch_negated=branch_negated,
   1275                     current_negated=current_negated, allow_joins=allow_joins,
-> 1276                     split_subq=split_subq,
   1277                 )
   1278                 joinpromoter.add_votes(needed_inner)

~/.virtualenvs/f4c2/lib/python3.6/site-packages/django/db/models/sql/query.py in build_filter(self, filter_expr, branch_negated, current_negated, can_reuse, allow_joins, split_subq, reuse_with_filtered_relation)
   1167         clause = self.where_class()
   1168         if reffed_expression:
-> 1169             condition = self.build_lookup(lookups, reffed_expression, value)
   1170             clause.add(condition, AND)
   1171             return clause, []

~/.virtualenvs/f4c2/lib/python3.6/site-packages/django/db/models/sql/query.py in build_lookup(self, lookups, lhs, rhs)
   1082             return
   1083 
-> 1084         lookup = lookup_class(lhs, rhs)
   1085         # Interpret '__exact=None' as the sql 'is NULL'; otherwise, reject all
   1086         # uses of None as a query value.

~/.virtualenvs/f4c2/lib/python3.6/site-packages/django/db/models/lookups.py in __init__(self, lhs, rhs)
     16     def __init__(self, lhs, rhs):
     17         self.lhs, self.rhs = lhs, rhs
---> 18         self.rhs = self.get_prep_lookup()
     19         if hasattr(self.lhs, 'get_bilateral_transforms'):
     20             bilateral_transforms = self.lhs.get_bilateral_transforms()

~/.virtualenvs/f4c2/lib/python3.6/site-packages/django/db/models/lookups.py in get_prep_lookup(self)
     66             return self.rhs._prepare(self.lhs.output_field)
     67         if self.prepare_rhs and hasattr(self.lhs.output_field, 'get_prep_value'):
---> 68             return self.lhs.output_field.get_prep_value(self.rhs)
     69         return self.rhs
     70 

~/.virtualenvs/f4c2/lib/python3.6/site-packages/django/db/models/fields/__init__.py in get_prep_value(self, value)
   1269     def get_prep_value(self, value):
   1270         value = super().get_prep_value(value)
-> 1271         return self.to_python(value)
   1272 
   1273     def get_db_prep_value(self, value, connection, prepared=False):

~/.virtualenvs/f4c2/lib/python3.6/site-packages/django/db/models/fields/__init__.py in to_python(self, value)
   1231 
   1232         try:
-> 1233             parsed = parse_date(value)
   1234             if parsed is not None:
   1235                 return parsed

~/.virtualenvs/f4c2/lib/python3.6/site-packages/django/utils/dateparse.py in parse_date(value)
     72     Return None if the input isn't well formatted.
     73     """
---> 74     match = date_re.match(value)
     75     if match:
     76         kw = {k: int(v) for k, v in match.groupdict().items()}

TypeError: expected string or bytes-like object

0 个答案:

没有答案