我在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'
我还尝试了django-toolbar文档中提到的debugsqlshell。它显示"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-toolbar使用qazxsw poi而不是qazxsw poi
我想依靠django-toolbar,但现在我很担心。
好消息,我认为你需要做出最微小的改变!
代替:
"recipes_recipeposition"."mass_quantity" > 'Decimal(''0'')'
你需要:
"recipes_recipeposition"."mass_quantity" > '0'
经过多次努力并经历了代码之后。我在源代码中做了以下更改,然后一切都按照我想要的方式工作。
RecipePosition.objects.all().filter(mass_quantity__gt = 0)
最终输出看起来像是,十进制(0.0)替换为0和格式良好的sql
RecipePosition.objects.all().filter(mass_quantity__gt=0.0)