0

Let's say this pseudocode represents models to work with

class Quiz():
    name = models.CharField()

class Answer():
    quiz = models.ForeignKey(Quiz)
    answer_text = models.CharField()

I would like to trace raw SQL, so let's also import from django.db import connection

print(Answer.objects.all().select_related('quiz').query)

ok, here we have inner join under the hood

>>> a = Answer.objects.all()
>>> print(a[0].quiz.query) 

however the latter example throws AttributeError: 'Quiz' object has no attribute 'query'

Could you tell if there are any ways of tracing raw sql more effectively? Which way is the best? I'm practicing more complicated prefetch_related and select_related queries. Thanks

fghfgg
  • 21
  • 4
  • 1
    Have you tried adding debug logging for your db https://stackoverflow.com/a/20161527/4151233? Or using the django debug toolbar? – Marco Jan 24 '22 at 01:49

1 Answers1

1

What This line a[0].quiz.query is trying to do is find a field name query in the model Quiz

.query only works with all() & filter(),

Polymath
  • 328
  • 1
  • 9