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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:19:05+00:00 2026-06-02T08:19:05+00:00

So, I have a model that looks like this: class Names(models.Model): name = models.CharField()

  • 0

So, I have a model that looks like this:

class Names(models.Model):
  name = models.CharField()

class Entries(models.Model):
  name = models.ForeignKey(Names)
  date = models.DateField()

The data looks something like this

name   |  date

name_1 |  2011-06-01
name_2 |  2011-03-01
name_3 |  2011-02-01
name_1 |  2010-06-01
name_2 |  2010-03-02
name_3 |  2010-02-01
name_4 |  2009-07-01

I want to query Entries with a date, and get each name record once where it is less than or equal to the date. So, I want this returned if I query with date 2011-06-01:

name_1 | 2011-06-01
name_2 | 2011-03-01
name_3 | 2011-02-01
name_4 | 2009-07-01

My assumption is this would do the trick:

date = datetime(year=2011, month=06, day=01)
results = Entries.objects.filter(date__lte=date).order_by('date').distinct('name')

But, I keep getting duplicate name entries in there.

Any tips, friends?

Edit: The solution was provided by Gareth Rees. I had to modify it slightly, but it looks like this:

sql = """myapp_entries.id = (SELECT E.id from myapp_entries AS E
                             WHERE E.name_id = myapp_names.id AND
                             '"""+date.strftime('%Y-%m-%d')+"""'
                             ORDER BY E.date DESC
                             LIMIT 1)'''

results = (Entries.objects
           .extra(where = [sql])
           .filter(date__lte = date)
           .order_by('name', 'date'))
  • 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-02T08:19:07+00:00Added an answer on June 2, 2026 at 8:19 am

    Here are a couple of approaches, depending on how you need the results.

    (1) If you don’t mind getting the results in dictionary form (rather than Entries objects) then you can use annotate combined with values to group the results. In this case the query you want would be something like this:

    from django.db.models import Min
    results = (Entries.objects
               .filter(date__lte = date)
               .values('name')
               .annotate(date = Min('date'))
               .order_by('name', 'date'))
    

    (2) If you need to get the results back as Entries objects, then you can use extra to pick only entries which are the earliest among all entries with the same name:

    sql = '''id = (SELECT E.id FROM myapp_entries AS E
                    WHERE E.name_id = myapp_entries.name_id
                    ORDER BY E.date
                    LIMIT 1)'''
    results = (Entries.objects
               .extra(where = [sql])
               .filter(date__lte = date)
               .order_by('name', 'date'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model that looks like this: class Category(models.Model): name = models.CharField(max_length=60) class
I have a Model that looks something like this: class Business(models.Model): name = models.CharField('business
I have a model that looks like this: class Invite(models.Model): user = models.ForeignKey(User) event
Have a class definition that looks something like this class someClass(models.Model): field1 = models.ForeignKey('other_app.field4')
I have a simple model which looks like this: class Group(models.Model): name = models.CharField(max_length
I have a Django model that looks something like this: class Person(models.Model): name =
In my application I have a model that looks like this: class Talk(models.Model): title
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have a Model that looks like: class MyModel(Model): name = models.TextField(blank=True, default='') bio
My django model looks like this: class Entity(models.Model): name = models.CharField(max_length=40) examples = models.ManyToManyField(Example,

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.