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

  • Home
  • SEARCH
  • 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 1029939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:37:26+00:00 2026-05-16T12:37:26+00:00

In django 1.2: I have a queryset with an extra parameter which refers to

  • 0

In django 1.2:

I have a queryset with an extra parameter which refers to a table which is not currently included in the query django generates for this queryset.

If I add an order_by to the queryset which refers to the other table, django adds joins to the other table in the proper way and the extra works. But without the order_by, the extra parameter is failing. I could just add a useless secondary order_by to something in the other table, but I think there should be a better way to do it.

What is the django function to add joins in a sensible way? I know this must be getting called somewhere.

Here is some sample code. It selects all readings for a given user, and annotates the results with the rating (if any) given by another user stored in ‘friend’.

class Book(models.Model):
    name = models.CharField(max_length=200)
    urlname = models.CharField(max_length=200)
    entrydate=models.DateTimeField(auto_now_add=True)

class Reading(models.Model):
    book=models.ForeignKey(Book,related_name='readings')
    user=models.ForeignKey(User)
    rating=models.IntegerField()
    entrydate=models.DateTimeField(auto_now_add=True)

readings=Reading.objects.filter(user=user).order_by('entrydate')

friendrating='(select rating from proj_reading where user_id=%d and \
book_id=proj_book.id and rating in (1,2,3,4,5,6))'%friend.id

readings=readings.extra(select={'friendrating':friendrating})

at the moment, readings won’t work because the join to readings is not set up correctly. however, if I add an order by such as:

.order_by('entrydate','reading__entrydate')

django magically knows to add an inner join through the foreign key and I get what I want.

additional information:

print readings.query ==>

select ((select rating from proj_reading where user_id=2 and book_id=proj_book.id and  rating in (1,2,3,4,5,6)) as 'hisrating', proj_reading.id, proj_reading.user_id, proj_reading.rating, proj_reading.entrydate from proj_reading where proj_reading.user_id=1;

assuming
user.id=1
friend.id=2

the error is:

OperationalError: Unknown column proj_book.id in 'where clause'

and it happens because the table proj_book is not included in the query. To restate what I said above – if I now do readings2=readings.order_by(‘book__entrydate’) I can see the proper join is set up and the query works.

Ideally I’d just like to figure out what the name of the qs.query function is that looks at two tables and figures out how they are joined by foreign keys, and just call that manually.

  • 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-16T12:37:27+00:00Added an answer on May 16, 2026 at 12:37 pm

    Your generated query:

    select ((select rating from proj_reading where user_id=2 and book_id=proj_book.id and rating in (1,2,3,4,5,6)) as ‘hisrating’, proj_reading.id, proj_reading.user_id, proj_reading.rating, proj_reading.entrydate from proj_reading where proj_reading.user_id=1;

    1. The db has no way to understand what does it mean by proj_book, since it is not included in (from tables or inner join).

    2. You are getting expected results, when you add order_by, because that order_by query is adding inner join between proj_book and proj_reading.

    As far as I understand, if you refer any other column in Book, not just order_by, you will get similar results.

    • Q1 = Reading.objects.filter(user=user).exclude(Book__name=”) # Exclude forces to add JOIN
    • Q2 = “Select rating from proj_reading where user_id=%d” % user.id
    • Result = Q1.extra(“foo”:Q2)

    This way, at step Q1, you are forcing DJango to add join on Book table, which is not default, unless you access any field of Book table.

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

Sidebar

Related Questions

I stumbled over this passage in the Django tutorial : Django models have a
I have a Django model with a large number of fields and 20000+ table
Does Django have any template tags to generate common HTML markup? For example, I
If I have a Django form such as: class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message
For my Django app I have Events, Ratings, and Users. Ratings are related to
I am running Django trunk and have template Autoescaping on (default). Do I need
I have written a Django app that makes use of Python threading to create
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have configured my Django app to run under Apache+FastCGI on Linux (Ubuntu, if
I have a pretty standard django app, and am wondering how to set the

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.