【问题标题】:Django toolbar: not showing the results for query with filter with greater thanDjango工具栏:不显示大于过滤器的查询结果
【发布时间】:2018-08-28 19:01:16
【问题描述】:

我在 Django 中有一个简单的查询。我安装了 Django 工具栏来检查 SQL 查询和相应的数据

我的模特:

class RecipePosition(models.Model):
    name = models.CharField(max_length=200,blank=True,help_text="If left blank will be same as Ingredient name Eg: Tomato pulp")
    mass_quantity = models.DecimalField(max_digits=19, decimal_places=10,null=True,blank=True,default=0,validators=[MinValueValidator(0)])
    title = models.CharField(max_length=200,blank=True)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

我有以下带有过滤器的 django 查询。

RecipePosition.objects.all().filter(mass_quantity__gt = 0)

Django获取mass_quantity大于0的所有对象。

但是当我检查 django 工具栏中的 sql 时。它显示:

SELECT "recipes_recipeposition"."id", 
       "recipes_recipeposition"."name",
       "recipes_recipeposition"."mass_quantity",
       "recipes_recipeposition"."title", 
       "recipes_recipeposition"."updated", 
       "recipes_recipeposition"."timestamp" 
FROM   "recipes_recipeposition" 
WHERE  "recipes_recipeposition"."mass_quantity" > 'Decimal(''0'')' 
ORDER  BY "recipes_recipeposition"."sequence_number" ASC 

我也在 sqlite 浏览器中尝试过这个命令,但它没有显示任何结果。

为什么 django-toolbar 没有显示正确的 SQL?

在我看来,sql应该是:

SELECT "recipes_recipeposition"."id", 
       "recipes_recipeposition"."name", 
       "recipes_recipeposition"."mass_quantity",
       "recipes_recipeposition"."title", 
       "recipes_recipeposition"."updated", 
       "recipes_recipeposition"."timestamp" 
FROM   "recipes_recipeposition" 
WHERE  "recipes_recipeposition"."mass_quantity" > 0 
ORDER  BY "recipes_recipeposition"."sequence_number" ASC 

这在 sqlite 浏览器中测试时会显示结果。

当我在 shell_plus 上用--print-sql --ipython 测试这个时,显示

$ python manage.py shell_plus --print-sql --ipython
System check identified some issues:

# Shell Plus Model Imports
from recipes.models import Recipe, RecipePosition

Python 3.6.4 (default, Jan  5 2018, 02:35:40) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: RecipePosition.objects.all().filter(mass_quantity__gt=0)
Out[1]: SELECT "recipes_recipeposition"."id",
       "recipes_recipeposition"."name",
       "recipes_recipeposition"."mass_quantity",
       "recipes_recipeposition"."title",
       "recipes_recipeposition"."updated",
       "recipes_recipeposition"."timestamp"
  FROM "recipes_recipeposition"
 WHERE "recipes_recipeposition"."mass_quantity" > '0'
 ORDER BY "recipes_recipeposition"."sequence_number" ASC
 LIMIT 21

仅在 django-toolbar 上它在 Django shell 上显示 Decimal() 东西,它显示 WHERE "recipes_recipeposition"."mass_quantity" > '0'

我还尝试了debugsqlshell,如 django-toolbar 文档中所述。它显示"recipes_recipeposition"."mass_quantity" > '0' 而不是"recipes_recipeposition"."mass_quantity" > 'Decimal(''0'')'

$ python manage.py debugsqlshell                   
Python 3.6.4 (default, Jan  5 2018, 02:35:40) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [2]: from recipes.models import Recipe, RecipePosition

In [3]: RecipePosition.objects.all().filter(mass_quantity__gt = 0)
Out[3]: SELECT "recipes_recipeposition"."id",
       "recipes_recipeposition"."name",
       "recipes_recipeposition"."mass_quantity",
       "recipes_recipeposition"."title",
       "recipes_recipeposition"."updated",
       "recipes_recipeposition"."timestamp"
FROM "recipes_recipeposition"
WHERE "recipes_recipeposition"."mass_quantity" > '0'
ORDER BY "recipes_recipeposition"."sequence_number" ASC
LIMIT 21 [1.58ms]

我不知道为什么 django-toobar 使用 "recipes_recipeposition"."mass_quantity" > 'Decimal(''0'')' 而不是 "recipes_recipeposition"."mass_quantity" > '0'

我想依赖 django-toolbar,但现在我很担心。

【问题讨论】:

  • 你用来构造mass_quantity的字段(DecimalField或IntegerField)
  • 请查看更新问题。它在 shell_plus 中显示良好,正如我所期望的那样,仅在 django-toolbar 中显示 Decimal() 的东西

标签: django


【解决方案1】:

好消息,我认为您需要做出最细微的改变!

代替:

RecipePosition.objects.all().filter(mass_quantity__gt = 0)

您需要:

RecipePosition.objects.all().filter(mass_quantity__gt=0.0)

【讨论】:

  • 没什么区别
  • @SanthoshYedidi 如果您使用我修改后的示例会怎样?
  • 现在问题出在语法上,问题出在 django-toolbar 上。
  • 能否请您提供错误跟踪,以便我可以帮助您调试。
  • 我刚刚发现它显示了一些错误的结果。
【解决方案2】:

终于经过大量的努力并完成了代码。我对源代码进行了以下更改,然后一切都按我想要的方式工作。

####################
http://127.0.0.1:8001/static/debug_toolbar/css/toolbar.css
# By doing this the sql will show in multiple lines with indent

#djDebug .djDebugSql {
    #word-break:break-word;
    z-index:100000002;
}

#####################

# replace \n with <br> and space with nbsp and dont use Boldkeyword
# By doing this the sql will show in multiple lines with indent

def reformat_sql(sql):
    stack = sqlparse.engine.FilterStack()
    options = formatter.validate_options({'reindent':True,'indent_width':True,})
    stack = formatter.build_filter_stack(stack, options)
    #stack.preprocess.append(BoldKeywordFilter())  # add our custom filter 
    stack.postprocess.append(sqlparse.filters.SerializerUnicode())  # tokens -> strings
    #return swap_fields(''.join(stack.run(sql)))
    return swap_fields(''.join(stack.run(sql)).replace("\n", "<br/>").replace(" ", "&nbsp;"))


#####################

in file  /lib/python3.6/site-packages/debug_toolbar/panels/sql/tracking.py
# because of this the greater than 0 is shown as deciman(0.0)   
# change for decimal wrap p in rev_typecast_decimal

def _record(self, method, sql, params):
    start_time = time()
    try:
        return method(sql, params)
    finally:
        stop_time = time()
        duration = (stop_time - start_time) * 1000
        if dt_settings.get_config()['ENABLE_STACKTRACES']:
            stacktrace = tidy_stacktrace(reversed(get_stack()))
        else:
            stacktrace = []
        _params = ''
        try:
            _params = json.dumps([self._decode(rev_typecast_decimal(p)) for p in params])
            #_params = json.dumps([self._decode(p) for p in params])  I  


###########################


            hare = []
            if params is not None:
                hare = [self._decode(rev_typecast_decimal(p)) for p in params]
                try:
                    _params = json.dumps([self._decode(rev_typecast_decimal(p)) for p in params])
                   # _params = json.dumps([self._decode(p) for p in params])
                except Exception:
                    pass  # object not JSON serializable

            template_info = get_template_info()

            alias = getattr(self.db, 'alias', 'default')
            conn = self.db.connection
            vendor = getattr(conn, 'vendor', 'unknown')

            params1 = {
                'vendor': vendor,
                'alias': alias,
                # 'sql': self.db.ops.last_executed_query(
                #     self.cursor, sql, self._quote_params(params)),
                'sql': self.db.ops.last_executed_query(
                     self.cursor, sql, hare),
                'duration': duration,
                'raw_sql': sql,
                'params': _params,
                'stacktrace': stacktrace,
                'start_time': start_time,
                'stop_time': stop_time,
                'is_slow': duration > dt_settings.get_config()['SQL_WARNING_THRESHOLD'],
                'is_select': sql.lower().strip().startswith('select'),
                'template_info': template_info,
            }




################################################

最终的输出看起来像,用小数(0.0)替换为0和格式良好的sql

SELECT "recipes_recipeposition"."id",
       "recipes_recipeposition"."name",
       "recipes_recipeposition"."recipe_id",
       "recipes_recipeposition"."ingredient_id",
       "recipes_recipeposition"."recipeposition_slug",
       "recipes_recipeposition"."cooking_unit",
       "recipes_recipeposition"."mass_unit_id",
       "recipes_recipeposition"."mass_quantity",
       "recipes_recipeposition"."volume_unit_id",
       "recipes_recipeposition"."volume_quantity",
       "recipes_recipeposition"."pieces_unit_id",
       "recipes_recipeposition"."pieces_quantity",
       "recipes_recipeposition"."cooking_notes",
       "recipes_recipeposition"."sequence_number",
       "recipes_recipeposition"."title",
       "recipes_recipeposition"."updated",
       "recipes_recipeposition"."timestamp",
       "ingredients_ingredient"."rate" AS "ingredient__rate",
       CASE
           WHEN "ingredients_ingredient"."munit" = 'kg' THEN 'kg'
           WHEN "ingredients_ingredient"."munit" = 'ltr' THEN 'ltr'
           WHEN "ingredients_ingredient"."munit" = 'pcs' THEN 'pcs'
           ELSE 'False'
       END AS "ingredient__cost_unit",
       CASE
           WHEN "ingredients_ingredient"."munit" = 'kg' THEN CASE
                                                                 WHEN ("recipes_recipeposition"."mass_unit_id" IS NOT NULL
                                                                       AND "recipes_recipeposition"."mass_quantity" IS NOT NULL
                                                                       AND "recipes_recipeposition"."mass_quantity" > '0') THEN CAST(("recipes_recipeposition"."mass_quantity" * "single_measurements_singlemeasurements"."quantity") AS NUMERIC)
                                                                 ELSE 'False'
                                                             END
           WHEN "ingredients_ingredient"."munit" = 'ltr' THEN 'ltr'
           WHEN "ingredients_ingredient"."munit" = 'pcs' THEN 'pcs'
           ELSE 'False'
       END AS "reciposition_cost_quantity"
FROM "recipes_recipeposition"
LEFT OUTER JOIN "ingredients_ingredient" ON ("recipes_recipeposition"."ingredient_id" = "ingredients_ingredient"."id")
LEFT OUTER JOIN "single_measurements_singlemeasurements" ON ("recipes_recipeposition"."mass_unit_id" = "single_measurements_singlemeasurements"."id")
WHERE "recipes_recipeposition"."recipe_id" = '1'
ORDER BY "recipes_recipeposition"."sequence_number" ASC

【讨论】:

    猜你喜欢
    • 2017-02-08
    • 1970-01-01
    • 2019-04-17
    • 2013-05-21
    • 2021-11-10
    • 2021-09-09
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多