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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:35:36+00:00 2026-06-14T03:35:36+00:00

In my django app, a logged in user can create an Entry which has

  • 0

In my django app, a logged in user can create an Entry which has the following attributes

from django.db import models
from datetime import date
from django.contrib.auth.models import User 
class Entry(models.Model):
    creationdate=models.DateField(default=date.today)
    description=models.TextField()
    author=models.ForeignKey(User,null=True)

In my view a user can retrieve all Entrys for a particular date as

def entries_on_ a_day(request,year,month,day):
    #month as 'jan','feb' etc
    ...
    entries_for_date = Entry.objects.filter(creationdate__year=year,creationdate__month=get_month_as_number(month),creationdate__day=day,author=request.user).order_by('-creationdate')
    ...

Now ,I need to use cache for this ,instead of doing a database hit everytime a user want to see a listing of Entrys on a day.How should I set the key for cache? Should I use a string made out of of username+creationdate as key?

from django.core.cache import cache

def entries_on_ a_day(request,year,month,day):
    creationdate=new date(year,get_month_as_number(month),day)
    key = request.user.username+ str(creationdate)
    if key not in cache:
        entries_for_date = Entry.objects.filter(creationdate__year=year,creationdate__month=get_month_as_number(month),creationdate__day=day,author=request.user).order_by('-creationdate')
        cache.set(key,entries_for_date)
    entries =  cache.get(key)
    ....
  • 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-14T03:35:37+00:00Added an answer on June 14, 2026 at 3:35 am

    Yes, you have the right idea. The general principle is that your cache key needs to be different for each query that might produce different results. Here, your query does only depend on creationdate and request.user, so as long as both of those are in the key then you’re set.

    However, you also need to make sure that the keys you generate for this function’s use of the cache are different from keys used by other parts of your Django deployment. So you should also include some kind of namespace. For example, something resembling this:

    "per-day-entries-{0}-{1}".format(request.user.username, creationdate)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model in my Django app: class Group(models.model): name=models.CharField(max_length=30) users=Models.ManyToManyField(User) In
My Django app has a Person table, which contains the following text in a
I have a page which displays a form that a logged-in user can use
Let's suppose I have the following url: /valid/django/app/path/?foo=bar&spam=eggs I can simulate a request to
My Django app displays data from a database. This data changes without user intervention,
I'm creating a Django app in which I have a table which has 'items',
I have a Django app. When logged in as an admin user, I want
I'm writing a Django app in which the users can upload images, and I'm
I have created a django app. It has got a user login/registration on the
In my Django App I have the following model: class SuperCategory(models.Model): name = models.CharField(max_length=100,)

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.