【发布时间】: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