Cadilac
Cadilac

Reputation: 1100

Looking for proper behavior

my models:

class Question(models.Model):
    content = models.CharField(max_length=160)

class Answer(models.Model):
    content = models.CharField(max_length=260)    
    question = models.ForeignKey(Question)

And if i want all answers related to particular question, but hiting database as little as possible, should i use select_related like that:

 q = Question.objects.select_related(depth=1).get(id=id)            
 answers = q.answer_set.all()

or just:

 q = Question.objects.get(id=id)            
 answers = q.answer_set.all()

?

Upvotes: 1

Views: 28

Answers (1)

Mechanical snail
Mechanical snail

Reputation: 30637

For answering this type of question, consider installing the Django Debug Toolbar. It provides information on all DB queries performed to process a request, including the amount of time each query takes.

So install the toolbar in your project, and browse to a page on your site that performs the query. Click on the toolbar to see query information. Try it both ways to see which is better.

Upvotes: 1

Related Questions