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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:47:56+00:00 2026-06-17T06:47:56+00:00

I have the following models: class Collection(models.Model): # some fields here … class Entry(models.Model):

  • 0

I have the following models:

class Collection(models.Model):
    # some fields here ...

class Entry(models.Model):
    collection = models.ForeignKey(Collection, reverse_name="entries")
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()

Now I want to query all Collections that have at least one Entry associated which I do with (quite quirky, I think):

collections_qs = Collection.objects.annotate(
    entry_count=Count('entries')
).filter(entry_count__gt=0)

Which works quite well. But I want to combine this query with a date/time window search given by the two datetimes start and end. What I’ve come up with was:

if start:
    collections_qs = collections_qs.filter(entries__start_time__gte=start)
if end:
    collections_qs = collections_qs.filter(entriess__end_time__lte=end)

But that just rearranges the order of the returned Collections but not the content. How can I achieve the date/time search (preferably with just one query)?

  • 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-06-17T06:47:57+00:00Added an answer on June 17, 2026 at 6:47 am

    For reverse FK (and ManyToMany fields too), there is a difference between

    .filter(a=b, x=y) and .filter(a=b).filter(x=y) (see doc).

    What you need when you have both start and end conditions is the filter(.,.) one that will get you the Collections that have at least one Entry that verifies both condition.

    Also, Collection.objects.filter(entries__isnull=False).distinct() seems more readable than .annotate(entry_count=Count('entries')).filter(entry_count__gt=0).

    Finally, when you have at least one condition on entries, you don’t need to have any filter to check for the existence of such entries.

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

Sidebar

Related Questions

Lets say we have the following models: (simplified) class Collection(models.Model): slug = models.SlugField() class
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
I have the following models: class ProjectUser(models.Model): categories = models.ManyToManyField('UserCategory', blank=True, null=True) user_id =
I have the following models class Person(models.Model): name = models.CharField(max_length=100) class Employee(Person): job =
I have the following models: class City(models.Model): name = models.CharField(max_length=100) class Pizza(models.Model): name =
I have the following models: class Profile(models.Model): verified = models.BooleanField(default=False) primary_phone = models.OneToOneField('Phone', related_name='is_primary',
I have a model class similar to the following: using System; using System.Collections.Generic; using
I have the following models: class Foo has_and_belongs_to_many :bars end class Bar has_and_belongs_to_many :foos
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following models: class FieldEntryValue < ActiveRecord::Base belongs_to :field_entry end and class

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.