如何反转SQL查询以访问其Django源?

时间:2015-06-10 11:04:15

标签: python mysql django python-2.7 django-models

是否可以反转复杂的sql查询(由连接和分组组成)以到达其Django源?

按来源,我的意思是可能触发查询的模型?

例如,考虑模型:

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

将触发在这些模型上使用group by和left outer join的查询。我能追溯一下吗?

1 个答案:

答案 0 :(得分:0)

May be this is the answer you are looking for.

Actually what i got from your comment 'I have a query and I need to know which models were involved when that query was triggered', you want to trace the models/tables which is getting involved in your query.

So for that you can use .query over your object.E.g;

exm = Example.objects.all()

print exm.query # This will give you the query which will be triggered in the back-end.

You can debug from your code or you can use django-debug-toolbar.