gpt4 book ai didi

Django : shell plus --print-sql showing incomplete sql if the query is long

转载 作者:行者123 更新时间:2023-12-01 11:15:30 26 4
gpt4 key购买 nike

我正在使用 Django 2.0。

我有以下型号:

class SingleMeasurements (models.Model):
MASS = 'kg'
VOLUME = 'ltr'
PIECES = 'pcs'
MUNITS_CHOICES = (
(VOLUME, 'Liter'),
(MASS, 'Kilogram'),
(PIECES, 'Pieces'),
)

name = models.CharField(max_length=200,unique=True,null=False)
slug = models.SlugField(unique=True)
formtype = models.CharField(max_length=10,choices=MUNITS_CHOICES,verbose_name="Units")
quantity = models.DecimalField(max_digits=19, decimal_places=10)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

class Recipe(models.Model):
MASS = 'kg'
VOLUME = 'ltr'
PIECES = 'pcs'
MUNITS_CHOICES = (
(MASS, 'Mass'),
(VOLUME, 'Volume'),
(PIECES, 'Pieces'),
)
name = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
tags = models.ManyToManyField(Tag,related_name='recipes_recipe_tag',blank=True)
primary_unit = models.CharField(max_length=10,choices=MUNITS_CHOICES,default=VOLUME,verbose_name="Preferred Display Units",null=True,blank=True)
mass_unit = models.ForeignKey(SingleMeasurements,on_delete=models.PROTECT,related_name='recipe_singlemeasurement_mass_unit',blank=True,null=True)
mass_quantity = models.DecimalField(max_digits=19, decimal_places=10,null=True,blank=True)
volume_unit = models.ForeignKey(SingleMeasurements,on_delete=models.PROTECT,related_name='recipe_singlemeasurement_volume_unit',blank=True,null=True)
volume_quantity = models.DecimalField(max_digits=19, decimal_places=10,null=True,blank=True)
pieces_unit = models.ForeignKey(SingleMeasurements,on_delete=models.PROTECT,related_name='recipe_singlemeasurement_pieces_unit',blank=True,null=True)
pieces_quantity = models.DecimalField(max_digits=19, decimal_places=10,null=True,blank=True)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)

我已经通过以下命令打开了 Django shell:
python manage.py shell_plus --print-sql --ipython

我正在尝试运行以下命令
Recipe.objects.select_related('mass_unit','volume_unit','pieces_unit').all()

输出是:
In [19]: Recipe.objects.select_related('mass_unit','volume_unit','pieces_unit').all()
Out[19]: SELECT "recipes_recipe"."id",
"recipes_recipe"."name",
"recipes_recipe"."slug",
"recipes_recipe"."primary_unit",
"recipes_recipe"."mass_unit_id",
"recipes_recipe"."mass_quantity",
"recipes_recipe"."volume_unit_id",
"recipes_recipe"."volume_quantity",
"recipes_recipe"."pieces_unit_id",
"recipes_recipe"."pieces_quantity",
"recipes_recipe"."updated",
"recipes_recipe"."timestamp",
"single_measurements_singlemeasurements"."id",
"single_measurements_singlemeasurements"."name",
"single_measurements_singlemeasurements"."slug",
"single_measurements_singlemeasurements"."formtype",
"single_measurements_singlemeasurements"."quantity",
"single_measurements_singlemeasurements"."updated",
"single_measurements_singlemeasurements"."timestamp",
T3."id",
T3."name",
T3."slug",
T3."formtype",
T3."quantity",
T3."updated",
T3."timestamp",
T4."id",
T4."name",
T4."slug",
T4."formtype",
T4."quantity",
T4."updated",
T4."timestamp"
FROM "recipes_recipe"
LEFT OUTER JOIN "single_measurements_singlemeasurements"
ON


Execution time: 0.001236s [Database: default]

我们可以看到 sql 是不完整的,它必须显示三个左内连接,但它停在 ON 处。

而如果我尝试:
Recipe.objects.select_related('mass_unit').all()

它显示了完整的 sql:
In [20]: Recipe.objects.select_related('mass_unit').all()
Out[20]: SELECT "recipes_recipe"."id",
"recipes_recipe"."name",
"recipes_recipe"."slug",
"recipes_recipe"."primary_unit",
"recipes_recipe"."mass_unit_id",
"recipes_recipe"."mass_quantity",
"recipes_recipe"."volume_unit_id",
"recipes_recipe"."volume_quantity",
"recipes_recipe"."pieces_unit_id",
"recipes_recipe"."pieces_quantity",
"recipes_recipe"."updated",
"recipes_recipe"."timestamp",
"single_measurements_singlemeasurements"."id",
"single_measurements_singlemeasurements"."name",
"single_measurements_singlemeasurements"."slug",
"single_measurements_singlemeasurements"."formtype",
"single_measurements_singlemeasurements"."quantity",
"single_measurements_singlemeasurements"."updated",
"single_measurements_singlemeasurements"."timestamp"
FROM "recipes_recipe"
LEFT OUTER JOIN "single_measurements_singlemeasurements"
ON ("recipes_recipe"."mass_unit_id" = "single_measurements_singlemeasurements"."id")
ORDER BY "recipes_recipe"."name" ASC
LIMIT 21

在这里它不会停止在 ON 处。它显示完整。

如何确保 --print-sql 显示完整的 sql。

最佳答案

将这些添加到您的 settings.py :

python manage.py shell_plus --print-sql :SHELL_PLUS_PRINT_SQL_TRUNCATE = {truncate limit} (默认为 1000)

python manage.py runserver_plus --print-sql :RUNSERVER_PLUS_PRINT_SQL_TRUNCATE = {truncate limit} (默认为 1000)

请注意,截断限制是将 sql 输出截断到的字符数,而不是行数。

关于 Django : shell plus --print-sql showing incomplete sql if the query is long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52015493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com