Django

Code

Ticket #1667 (new)

Opened 3 years ago

Last modified 1 year ago

OneToOne and ForeignKey related objects are cached differently

Reported by: Maniac <Maniac@SoftwareManiacs.Org> Assigned to: nobody
Milestone: Component: Database layer (models, ORM)
Version: SVN Keywords: 121-rewrite
Cc: Triage Stage: Design decision needed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Looks like ForeignKeys? respect model's custom default manager and OneToOneField? don't. Here's an example.

There are 3 models: Author, Article and Contact. Article refers to Author with ForeignKey? and Contact refers to Author with OneToOneField?. Author has a custom default manager that gives out only authors with their 'available' field set to True:

class AvailableAuthorManager(models.Manager):
  def get_query_set(self):
    return super(AvailableAuthorManager, self).get_query_set().filter(available__exact=True)

class Author(models.Model):
  name = models.CharField(maxlength=50)
  available = models.BooleanField(default=True)
  
  objects = AvailableAuthorManager()
  
class Article(models.Model):
  author = models.ForeignKey(Author)
  title = models.CharField(maxlength=50)
  
class Contact(models.Model):
  author = models.OneToOneField(Author)
  address = models.CharField(maxlength=50)

Then if you have an author with available=False and an article and a contact reefering to it you get:

  • article.author -- raises an exception DoesNotExist?
  • contact.author -- returns the author

Looks like this should behave the same way in both cases. However I'm not sure how exactly but I tend to think that it should raise an exception because managers are supposed to be the ultimate mechanism to access model objects.

Attachments

Change History

04/21/06 02:08:24 changed by ubernostrum

Using the models and manager outlined here, I created one Author, and an Article and a Contact related to it. Then I set the Author's 'available' field to False and got a DoesNotExist? exception for both 'article.author' and 'contact.author'. Maybe we're on different revisions and something changed between them?

04/21/06 02:28:08 changed by Maniac <Maniac@SoftwareManiacs.Org>

  • summary changed from OneToOne and ForeignKey treat managers differently to OneToOne and ForeignKey related objects are cached differently.

I tested it further and it seems you're right. The behavior that I was observing was caused by caching objects:

author = Author(name='...')
author.save()
article = Article(author=author, title='...')
article.save()
contact = Contact(author=author, address='...')
contact.save()

article.author  # OK
contact.author  # OK

author.available=False
author.save()

article.author  # Exception
contact.author  # OK

So it seems that accessing object through OneToOne? does use cached object and ForeignKey? does a query. I'm changing summary appropriately. Now I think cached version should be used in both cases because caches are usually expected to be out of sync with reality.

04/21/06 02:41:40 changed by Maniac <Maniac@SoftwareManiacs.Org>

Here's more. If you get a contact or an article with select_related() you will always have their authors. I mean selected_related() seems to not use managers at all.

I sorts understand why is this happening but I'm not very familiar with this code. May be I should file another bug for this?

06/12/06 06:55:31 changed by Link

  • type deleted.

06/19/06 20:58:57 changed by anonymous

  • type set to defect.

07/04/06 22:45:40 changed by hi-world cup

  • cc changed from Maniac@SoftwareManiacs.Org to hi-world, cup.
  • keywords set to rthml tab space editor js.
  • summary changed from OneToOne and ForeignKey related objects are cached differently to hi-world cup.

07/04/06 22:55:42 changed by adrian

  • summary changed from hi-world cup to OneToOne and ForeignKey related objects are cached differently.

02/17/07 23:37:08 changed by Gary Wilson <gary.wilson@gmail.com>

  • cc deleted.
  • keywords deleted.
  • stage changed from Unreviewed to Design decision needed.

06/07/07 16:57:13 changed by adrian

  • version changed from magic-removal to SVN.

11/30/07 15:28:12 changed by jacob

  • keywords set to 121-rewrite.

Add/Change #1667 (OneToOne and ForeignKey related objects are cached differently)




Change Properties
Action