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

The Archive Base Latest Questions

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

I am having trouble in django on how to retrieve data from last week

  • 0

I am having trouble in django on how to retrieve data from last week (not 7 days ago). Using date.isocalendar()[1] would be great. However Some stackoverflow browsing lead me to no satisfactory results.

Whatsoever, I could do without portability and use mysql’s INTERVAL function. This is the query I want to make using django’s ORM.

SELECT id, user_id, CAST(timestamp AS Date), WEEK(timestamp,3), WEEK(CURDATE(), 3) FROM main_userstats WHERE week(timestamp, 3) = WEEK(DATE_SUB(CURDATE(), INTERVAL 1 WEEK ), 3)

how can I do this using the extra function in django (if it’s not possible to do in any other simpler way)?

  • 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-12T06:56:51+00:00Added an answer on June 12, 2026 at 6:56 am

    I assume what you are looking for are all the entries that belong to the same calendar week of the previous week.

    This should do the trick:

    class Entry(models.Model):
        pub_date = models.DateField([...])
    

    To get the objects:

    from datetime import timedelta
    from django.utils import timezone
    some_day_last_week = timezone.now().date() - timedelta(days=7)
    monday_of_last_week = some_day_last_week - timedelta(days=(some_day_last_week.isocalendar()[2] - 1))
    monday_of_this_week = monday_of_last_week + timedelta(days=7)
    Entry.objects.filter(created_at__gte=monday_of_last_week, created_at__lt=monday_of_this_week)
    

    Note that I added 7 days to get the monday of the this week instead of adding 6 days to get the sunday of last week and that I used created_at__lt=monday_of_this_week (instead of __lte=). I did that because if your pub_date was a DateTimeField, it wouldn’t include the sunday objects since the time is 00:00:00 when using now().date().

    This could easily be adjusted to consider Sunday as the first day of the week instead, but isocalendar() considers it the last, so I went with that.

    If using Django < 1.4 use the following:

    from datetime import date, timedelta
    some_day_last_week = date.today() - timedelta(days=7)
    

    instead of:

    from datetime import timedelta
    from django.utils import timezone
    some_day_last_week = timezone.now().date() - timedelta(days=7)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble pulling comments into my template using django comments. I'd like to
Having trouble with the db.alter command when changing a date field from null=True and
I'm having trouble with sending localized messages to Django users using the user.message_set.create(message=Message) mechanism.
I'm having trouble using Markdown in Django Nonrel. I followed this instructions (added 'django.contrib.markup'
Trying to work with a trivial navigation menu using django templates, I'm having trouble
I'm using django-compress to shrink my JavaScript files. However, I am now having trouble
I'm having trouble using python function decorators in Google's AppEngine. I'm not that familiar
I am using django-selectable and having trouble saving the id ('autocomplete_1') which represents the
I have started using django-piston for creating APIS but I am having trouble finding
I'm using Django and I am having trouble passing variable information back to the

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.