【问题标题】:djongo.sql2mongo.SQLDecodeError: FAILED SQL: Error in django while filtering Datadjongo.sql2mongo.SQLDecodeError: FAILED SQL: 过滤数据时 django 出错
【发布时间】:2020-01-31 09:24:08
【问题描述】:

我正在尝试过滤价格低于过滤器但出现错误的产品列表。我猜错误是我无法正确传递参数。 任何帮助将不胜感激。

如果您需要更多详细信息,请询问。

注意:即使我在过滤器主体静态上传递查询,它也不起作用。

提前致谢。

我的方法:

def getfilter(request, format=None):
    kwargs = {
    '{0}__{1}'.format('price', 'lt'): Decimal('1000.00'),
    # '{0}__{1}'.format('name', 'endswith'): 'Z'
}

    products = Product.objects.all().filter(**kwargs)
    serializer = ProductSerializer1(products, many=True)
    return Response(serializer.data)

我收到错误:

Traceback (most recent call last):
  File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 806, in parse
    return handler(self, statement)
  File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 934, in _select
    self._query = SelectQuery(self.db, self.connection_properties, sm, self._params)
  File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 113, in __init__
    super().__init__(*args)
  File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 74, in __init__
    self.parse()
  File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 153, in parse
    raise SQLDecodeError
djongo.sql2mongo.SQLDecodeError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/sunil/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/sunil/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/sunil/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/sunil/.local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/sunil/.local/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
    response = self.handle_exception(exc)
  File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/decorators.py", line 55, in handler
    return func(*args, **kwargs)
  File "/home/sunil/Projects/python/loginBazaar/products/prodView.py", line 219, in getfilter
    return Response(serializer.data)
  File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/serializers.py", line 768, in data
    ret = super(ListSerializer, self).data
  File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data
    self._data = self.to_representation(self.instance)
  File "/home/sunil/.local/lib/python3.6/site-packages/rest_framework/serializers.py", line 686, in to_representation
    self.child.to_representation(item) for item in iterable
  File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/query.py", line 274, in __iter__
    self._fetch_all()
  File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/sunil/.local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
    cursor.execute(sql, params)
  File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/sunil/.local/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/sunil/.local/lib/python3.6/site-packages/djongo/cursor.py", line 53, in execute
    params)
  File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 743, in __init__
    self.parse()
  File "/home/sunil/.local/lib/python3.6/site-packages/djongo/sql2mongo/query.py", line 826, in parse
    raise exe from e
djongo.sql2mongo.SQLDecodeError: FAILED SQL: SELECT "products_product"."id", "products_product"."name", "products_product"."vendorId", "products_product"."quantity", "products_product"."shopCode", "products_product"."discountPercent", "products_product"."discountPrice", "products_product"."price", "products_product"."dummyPrice", "products_product"."margin", "products_product"."description", "products_product"."is_verified", "products_product"."category", "products_product"."subCategory", "products_product"."images", "products_product"."sizeChart", "products_product"."attributes", "products_product"."buyCount", "products_product"."thumbnail", "products_product"."productCode", "products_product"."atts" FROM "products_product" WHERE "products_product"."price" < %(0)s ORDER BY "products_product"."productCode" ASC
Version: 1.2.32

【问题讨论】:

    标签: python django python-3.x djongo


    【解决方案1】:

    十进制到字符串的转换好像失败了:

    '{0}__{1}'.format('price', 'lt'): Decimal('1000.00'),
    

    及对应的SQL:

    < %(0)s
    

    我猜这是可能的麻烦之一:

    • 您的模型字段 price 类型对于此转换不正确
    • 您的十进制值在小数点后有两个零数字 - 尝试 删除最后一个

    【讨论】:

    • 我认为转换也失败了。这就是该字段的定义方式。:-> price = models.DecimalField(default=0.00,decimal_places=2,max_digits=10)
    • 如果您从过滤器中的十进制值中删除最后一个零位或将转换显式转换为字符串str(Decimal("1000.00")),会发生什么?
    • 然后什么都不会发生。
    • 我想知道数据库中的字段类型是什么。您使用的是 MongoDB,对吗?
    • 是的 mongodb。并在数据库中显示为字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2013-07-18
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 2012-01-13
    相关资源
    最近更新 更多