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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:50:59+00:00 2026-05-23T08:50:59+00:00

Using the example Models from https://docs.djangoproject.com/en/dev/topics/db/queries/ I want to do something like the following:

  • 0

Using the example Models from https://docs.djangoproject.com/en/dev/topics/db/queries/

I want to do something like the following:

q = Entry.objects.filter(...)
entries = q.all().order_by('pub_date')
blog_counts = q.values('blog__name').annotate(Count('blog'))

This gives me back a result like the following:

[{'blog__count': 3, 'blog__name': u'Cheddar Talk'}, {'blog__count': 5, 'blog__name': u'Beatles Blog'}]

If the Entry.blog ForeignKey is made “null=True, blank=True”, and I create several Entry records with a null value for blog, I get something like this:

[{'blog__count: 0, 'blog__name': None}, {'blog__count': 3, 'blog__name': u'Cheddar Talk'}, {'blog__count': 5, 'blog__name': u'Beatles Blog'}]

I expected to get the actual number of entries with no Blog associated, not 0. Looking at the SQL executed, I see that it is doing COUNT(blog.id) instead of something like COUNT(entry.blog_id), but changing to .annotate(Count(‘blog_id’)) gives me:

FieldError: Cannot resolve keyword 'blog_id' into field.

What can I do to get an accurate count of Entries without an associated Blog?

  • 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-23T08:51:00+00:00Added an answer on May 23, 2026 at 8:51 am
    Entry.objects.filter(blog__isnull=True).count()
    

    That will give you what you want. In your current code, Count is giving you exactly what you asked for, the number of blogs each entry is associated with. In the case of the ones with null blogs, that’s zero, obviously.

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

Sidebar

Related Questions

I am using the example from TheElements sample code provided with the iPhone SDK
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
Fresh from the Djangobook tutorial using the Books app example, you have Book related
I'm trying to create a validator for my models: But taking the example from
Using the example code included in the man page for DateTime::Astro::Sunrise , I'm getting
For example: using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.Web.Script.Serialization;
I am using the example on the AJAX website for the DropDownExtender . I'm
i'm using this example implementation found at http://tangentsoft.net/wskfaq/examples/basics/select-server.html This is doing most of what
I am using UICatalog example and trying to find whether it is possible to
Does anyone have an example using the table object in YUI library. More specifically,

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.