0

Suppose i have :

models.py:

class Books(models.Model):

    title = models.CharField(max_length = 200)
    author = models.CharField(max_length = 200)
    price = models.CharField(max_length = 200)

and in manage.py shell :

# hits the database
>>> book_one = Books.objects.get(id = 1)
# hits the database 
>>> foo = book_one.title
u'Foo book'

assuming on the code above, if i type book_one.author we're gonna hit the database again.

By using select_related() if i type book_one.title or book_one.author or book_one.price we're not gonna hit the database again, only when we instance it.

If i type books = Books.objects.all() in manage.py shell and let's say i want to store its specific value (let's say i only want to store title and author field) in an empty list using for loop like :

empty_list = []
for book in books:
    empty_list.append([book.title, book.author])
# only 1 data, so we're gonna loop only once

So the question is : based on that loop above, how many times we're gonna hit the database? Is it 2 times because we pass book.title and book.author or only once

4
  • 1
    you will not hit the database again for book_one.title or book_one.author Commented Sep 13, 2012 at 11:35
  • really? because the docs says it's gonna hit the database again docs.djangoproject.com/en/1.4/ref/models/querysets/… Commented Sep 13, 2012 at 11:37
  • what i meant was about your first statement.. # hits the database >>> book_one = Books.objects.get(id = 1) # hits the database -- [no it will not ] >>> book_one.title u'Foo book' Commented Sep 13, 2012 at 11:39
  • select_related is for related objects (foreign keys) Commented Sep 13, 2012 at 11:41

3 Answers 3

2

assuming on the code above, if i type book_one.author we're gonna hit the database again.

Incorrect. All simple fields in a model are pulled from the database unless .defer() or .only() are used.

By using select_related() if i type book_one.title or book_one.author or book_one.price we're not gonna hit the database again, only when we instance it.

select_related() only affects related fields, e.g. ForeignKey, OneToOneField, etc.

based on that loop above, how many times we're gonna hit the database?

Once. But consider using .values_list('title', 'author') instead.

Sign up to request clarification or add additional context in comments.

Comments

0

If you have a class

class Books(models.Model):

    title = models.CharField(max_length = 200)
    author = models.CharField(max_length = 200)
    price = models.CharField(max_length = 200)

Now,

>>> book_one = Books.objects.get(id = 1) # hits the database
>>> foo = book_one.title # does not hit the database

if at all title was a foreign key to another table then, foo = book_one.title would have hit the database and using select_related would have saved you this database hit

Comments

0

Your assumption is not correct. Querysets are lazy, and are only executed when evaluated.

f = Books.objects.get(pk=1) # one hit
f.title # no hit
f.price # no hit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.