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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:54:39+00:00 2026-05-30T02:54:39+00:00

I am trying to count daily records for some model, but I would like

  • 0

I am trying to count daily records for some model, but I would like the count was made only for records with some fk field = xy so I get list with days where there was a new record created but some may return 0.

class SomeModel(models.Model):
    place = models.ForeignKey(Place)
    note = models.TextField()
    time_added = models.DateTimeField()

Say There’s a Place with name=”NewYork”

data = SomeModel.objects.extra({'created': "date(time_added)"}).values('created').annotate(placed_in_ny_count=Count('id'))

This works, but shows all records.. all places.

Tried with filtering, but it does not return days, where there was no record with place.name="NewYork". That’s not what I need.

  • 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-30T02:54:40+00:00Added an answer on May 30, 2026 at 2:54 am

    It looks as though you want to know, for each day on which any object was added, how many of the objects created on that day have a place whose name is New York. (Let me know if I’ve misunderstood.) In SQL that needs an outer join:

    SELECT m.id, date(m.time_added) AS created, count(p.id) AS count
      FROM myapp_somemodel AS m
      LEFT OUTER JOIN myapp_place AS p
           ON m.place_id = p.id
           AND p.name = 'New York'
      GROUP BY created
    

    So you can always express this in Django using a raw SQL query:

    for o in SomeModel.objects.raw('SELECT ...'):   # query as above
        print 'On {0}, {1} objects were added in New York'.format(o.created, o.count)
    

    Notes:

    1. I haven’t tried to work out if this is expressible in Django’s query language; it may be, but as the developers say, the database API is “a shortcut but not necessarily an end-all-be-all.”)

    2. The m.id is superfluous in the SQL query, but Django requires that “the primary key … must always be included in a raw query”.

    3. You probably don’t want to write the literal 'New York' into your query, so pass a parameter instead: raw('SELECT ... AND p.name = %s ...', [placename]).

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

Sidebar

Related Questions

I'm trying to count how many distinct value of FLOOR there is but I
I'm trying to count the number of records that have a null DateTime value.
I'm trying to count the days between two dates, excluding Saturdays and Sundays .
I am trying to count the number of new records for a given date
I'm trying to count the total number of rows that would have been returned
I am trying to floss daily, get my exercise, and make sure I maintain
I'm trying to count all User with empty last_name. I'm trying those but they
Im trying use count case to count records in a certain column (runs.Finish) if
I am trying to count the number of days of the current week that
I am trying to COUNT two columns in a single query, but the results

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.