简体   繁体   English

如何在django中打印.get queryset的查询

[英]How to print the query of .get queryset in django

How can I know the query when doing a .get queryset in django 在django中执行.get查询集时如何知道查询

I have this model: 我有这个型号:

class Artist(EsIndexable, models.Model):
    name = models.CharField(max_length=50)
    birth_date = models.DateField()

And I did this in the shell: 我在shell中做了这个:

x = Artist.objects.get(name="Eminem")
print x.query

Then I got the error: 然后我得到了错误:

AttributeError: 'Artist' object has no attribute 'query'

.get returns an instance, not a queryset. .get返回一个实例,而不是一个查询集。

To see the query that is done, do the same thing but with .filter , which does return a queryset: 要查看已完成的查询,请执行相同的操作,但使用.filter ,它会返回一个查询集:

queryset = Artist.objects.filter(name="Eminem")
print queryset.query
x = queryset.get()
from django.db import connection

x = Artist.objects.get(name="Eminem")
print connection.queries[-1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM