Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6581283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:07:59+00:00 2026-05-25T16:07:59+00:00

Having the model: class Notebook(models.Model): n_id = models.AutoField(primary_key = True) class Note(models.Model): b_nbook =

  • 0

Having the model:

class Notebook(models.Model):
    n_id = models.AutoField(primary_key = True)

class Note(models.Model):
    b_nbook = models.ForeignKey(Notebook)

the URL pattern passing one parameter:

(r'^(?P<n_id>\d+)/$', 'notebook_notes')

and the following view:

def notebook_notes(request, n_id):
    nbook = get_object_or_404(Nbook, pk=n_id)
...

which of the following is the optimum query set, and why? (they both work and pass the notes based to a selected by URL notebook)

notes = nbook.note_set.filter(b_nbook = n_id)
notes = Note.objects.select_related().filter(b_nbook = n_id)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:08:00+00:00Added an answer on May 25, 2026 at 4:08 pm

    Well you’re comparing apples and oranges a bit there. They may return virtually the same, but you’re doing different things on both.

    Let’s take the relational version first. That query is saying get all the notes that belong to nbook. You’re then filtering that queryset by only notes that belong to nbook. You’re filtering it twice on the same criteria, in effect. Since Django’s querysets are lazy, it doesn’t really do anything bad, like hit the database multiple times, but it’s still unnecessary.

    Now, the second version. Here, you’re starting with all notes and filtering to just those that belong to the particular notebook. There’s only one filter this time, but it’s bad form to do it this way. Since it’s a relation, you should look it up through the relational format, i.e. nbook.note_set.all(). On this version, though, you’re also using select_related(), which wasn’t used on the other version.

    select_related will attempt to create a join table with any other relations on the model, in this case a Note. However, since the only relation on Note is Notebook and you already have the notebook, it’s redundant.

    Taking out all the redundancy in those two version leaves you with just:

    notes = nbook.note_set.all()
    

    That, too, will return exactly the same results as the other two version, but is much cleaner and standardized.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a little django problem i'm stuck with... My Model: class Mymodel(models.Model): [...] user
I'm having a FieldError . I have a model: class Foo(models.Model): __bar = models.TextField(default=lambda:
Imagine this model: class ExercisePart(models.Model): exercise = models.ForeignKey(Exercise) sequence = models.IntegerField() class Meta: unique_together
I am currently having a model: class Current(models.Model): field1 = models.IntegerField() field2 = models.IntegerField()
Greetings, Assume I have such model: class Foo(models.Model): type = models.ForeignKey(Type) start_time = models.DateTimeField()
I'm having a problem testing the following model: class Bill < ActiveRecord::Base belongs_to :consignee
When designing both the domain-model and class-diagrams I am having some trouble understanding what
model: class Product(models.Model): name = models.CharField(max_length = 128) (...) def __unicode__(self): return self.name class
Having this model class public class Usuario { #region Atributos private int _intID =
I am having difficulties implementing the codeigniter pagination class. I have created my model,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.