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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:25:27+00:00 2026-06-15T16:25:27+00:00

I struggle with some caching issue inside Django. So far I’ve seen this issue

  • 0

I struggle with some caching issue inside Django. So far I’ve seen this issue only when running testsuite. The problem is that sometimes (it seems that this happens always on second invocation of the code), Django does not update it’s cache or it becomes inconsistent.

The extracted code with some debugging is:

class Source(models.Model):
    name = models.CharField(max_length = 50)
    quality = models.IntegerField(default = 0)

class Reference(models.Model):
    url = models.URLField()
    source = models.ForeignKey(Source)

    class Meta:
        ordering = ['-source__quality']

class Issue(models.Model):
    references = models.ManyToManyField(Reference)
    master = models.ForeignKey(Reference, related_name = 'mastered_issue_set')

def auto_create(instance):
    issue = Issue.objects.create(master = instance)
    print issue.references.count(), issue.references.all()
    issue.references.add(instance)
    print issue.references.count(), issue.references.all()

At first invocation I correctly get following output:

0 []
1 [<Reference: test>]

However in second call to to auto_create, Django thinks there is one reference, but it does not give it to me:

0 []
1 []

This behavior of course breaks further code. Any idea what can be going wrong here or at least how to debug it?

PS: It looks like ordering on Reference class is causing this. But it is still unclear to me why.

  • 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-15T16:25:29+00:00Added an answer on June 15, 2026 at 4:25 pm

    In the end I’ve found what is causing this problem. It was my own caching code rather than Django’s.

    I had custom Source manager in place, which returned and cached some standard source:

    class SourceManager(models.Manager):
        url_source = None
        def get_generic(self):
            if self.url_source is None:
                self.url_source, created = self.get_or_create(name = 'URL', quality = 0)
            return self.url_source
    
    class Source(models.Model):
        name = models.CharField(max_length = 50)
        quality = models.IntegerField(default = 0)
    
        objects = SourceManager()
    

    This works perfectly fine in the application – once source is created, the manager remembers it for it’s existence as the sources do not change over their lifetime. However in tests they go away as the whole test is run in single transaction and then reverted.

    What I find strange is that models.ForeignKey did not complain about getting non existing object, but the error appeared later, while sorting by source__quality as the underlaying JOIN SELECT could not find matching Source object.

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

Sidebar

Related Questions

struggle extracting necessary data from this complex json stored inside php variable. Via php
I'm struggle extracting necessary data from this json stored inside php variable. Not yet
I'm having a little struggle on this one and would appreciate some help. In
After some struggle with smooks bean binding and CSV mapping, I managed to parse
I am trying to call a function, but am running into some struggles with
We Really need some help on this one: We've Struggled with all the Apple
Last two nights I am struggle with below code. The problem is I need
I want to make a program that will involve some faces with various facial
I'm doing some research on GPGPU and currently struggle with the question what Tesla
Most projects have some sort of data that are essentially static between releases and

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.