1

I have 2 models > Shelf and Book in my models.py. The model Book has a field ReferenceProperty(Shelf), it also has a IntegerProperty field that stores the no. of pages in the book. What i am trying to achieve is to get a list of Top 25 Shelf Names according to highest number of Pages (which should be the sum of pages of all the books in that shelf) in descending order.

i am a beginner with python programming. Please advise me.

1
  • 2
    there is no straight forward way to query what you want with a model relationship like this. there should be a pagesnumber property on the shelf so you could query on shelves and order them by pagenumber. Commented May 15, 2012 at 13:16

1 Answer 1

3

On App Engine, I think the best way to do this is to store the total pages inside the Shelf. Add an IntegerProperty field to the shelf, I'll call it totalPages. Every time you add or remove a book to the shelf, update totalPages appropriately. Note that this will need to be done in a transaction.

Then it's easy to search the Shelf objects by totalPages.

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

1 Comment

Thankyou for your response, that is what i have pretty much done. Added a TotalPages integer property to the shelf model, however i was facing a problem when any of the book data was removed from the book table so i added another function to update TotalPages in a Shelf and added it to the task queue which we'll be running every 7-10 days to update the correct no.

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.