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 348697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:23:20+00:00 2026-05-12T11:23:20+00:00

Not even sure I’m stating the question correctly. Here’s the situation. I have a

  • 0

Not even sure I’m stating the question correctly. Here’s the situation. I have a queryset generated by accessing the foreign key relationship. Using the standard Blog/Entry models in the Django documentation, let’s say I have selected a blog and now have a set of entries:

entries = Blog.objects.get(id=1).entry_set.all()

So we have some entries for the blog, possibly zero. I’d like to then say construct a calendar and indicate which days have blog entries. So my thinking is to iterate over list of days in the month or whatever, and check the entries queryset for an entry with that date. Question is, what is the best way to do this? My first thought was to do something like

dayinfo = [] # we will iterate over this in the template
for curday in month:
  dayinfo.append({'day':curday, 'entry':entries.filter(day=curday)})

Problem is that the filter call returns a new queryset, and that generates a new sql call for each loop iteration. I just need to pluck the entry object from entries if it exists and stick it into my calendar. So what is the best way to do this? I did get this to work:

dayinfo.append({'day':day, 'entry':[e for e in entries if e.day == curday][0]})

That does not generate new sql calls. But it sure seems ugly.

  • 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-12T11:23:21+00:00Added an answer on May 12, 2026 at 11:23 am

    Resist the urge to put everything on one line – I think the code would be cleaner with something like this:

    from collections import defaultdict
    calendar = defaultdict(list)
    
    for entry in entries:
        calendar[entry.day].append(entry)
    

    The defaultdict part is simple but you might want to initialize it with all of the days in a month if you’re just planning to use a for loop in the template. Also note that if you’re using Django 1.1 you can use the new annotate() method to simply calculate the post count if you’re not actually planning to generate links to individual posts.

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

Sidebar

Related Questions

I'm not sure if this is even possible but here goes: Currently I have
Not even sure if this is feasible, but here's the use case: I have
I'm not even sure if it's possible but say I have some XML: <source>
Not even sure if this is the right way to title the question. I
I'm not even sure this is possible to do efficiently, but here's my problem:
I am not even sure my thread title is correct or not. Here is
Not even sure if methods is the correct terminology... Here is the original working
I'm not even sure if this is possible or not, but here's what I'd
I'm not sure what I am doing wrong here, not even sure if I
I'm not even sure where to begin with this question... I want to be

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.