Django

Code

Ticket #8070 (closed: fixed)

Opened 4 months ago

Last modified 4 months ago

related objects passed to model init are not cached/accessible

Reported by: gwilson Assigned to: gwilson
Milestone: 1.0 Component: Core framework
Version: SVN Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Models:

from django.db import models

class Color(models.Model):
    name = models.CharField(max_length=10)

class Knight(models.Model):
    favorite = models.ForeignKey(Color)

Neither saved or unsaved related objects get cached:

# Unsaved object.
>>> k = Knight(favorite=Color())
>>> hasattr(k, '_favorite_cache')
False

# Saved object.
>>> k = Knight(favorite=Color.objects.create(name='blue'))
>>> hasattr(k, '_favorite_cache')
False

# But assignment with saved or unsaved objects does cache correctly.
>>> k = Knight()
>>> k.favorite = Color()
>>> hasattr(k, '_favorite_cache')
True
>>> k = Knight()
>>> k.favorite = Color.objects.create(name='blue')
>>> hasattr(k, '_favorite_cache')
True

Also, in the saved related object case above, we do an extra query to refetch the object by id.

There's also a problem when trying to access the field when it was passed an unsaved object in the init:

# Unsaved object.
>>> k = Knight(favorite=Color())
>>> k.favorite
Traceback (most recent call last):
...
DoesNotExist

# Saved object.
>>> k = Knight(favorite=Color.objects.create(name='blue'))
>>> k.favorite
<Color: blue>

The attached patch does both:

  • Caches saved or unsaved related object passed to the Model init.
  • Allows unsaved related objects to be accessed when they were passed to the Model init.

Attachments

8070.patch (5.4 kB) - added by gwilson on 08/01/08 02:04:14.
8070.2.patch (6.4 kB) - added by gwilson on 08/01/08 02:07:13.
corrected a comment

Change History

08/01/08 02:04:14 changed by gwilson

  • attachment 8070.patch added.

08/01/08 02:07:13 changed by gwilson

  • attachment 8070.2.patch added.

corrected a comment

08/01/08 02:08:57 changed by gwilson

  • status changed from new to assigned.
  • needs_better_patch changed.
  • needs_tests changed.
  • owner changed from nobody to gwilson.
  • needs_docs changed.
  • stage changed from Unreviewed to Ready for checkin.

Someone else care to review?

08/01/08 02:15:19 changed by gwilson

Patch note: #6886 made the type checking happen on assignment, so we don't need to do the checking in the init anymore as long as you are setting using the attribute named by field.name.

08/01/08 18:17:00 changed by gwilson

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [8185]) Fixed #8070 -- Cache related objects passed to Model init as keyword arguments. Also:

  • Model init no longer performs a database query to refetch the related objects it is passed.
  • Model init now caches unsaved related objects correctly, too. (Previously, accessing the field would raise DoesNotExist error for null=False fields.)
  • Added tests for assigning None to null=True ForeignKey fields (refs #6886).

Add/Change #8070 (related objects passed to model init are not cached/accessible)




Change Properties
Action