#10695 closed (fixed)
defer() uses the same cached value for all models
| Reported by: | jbronn | Owned by: | Malcolm Tredinnick |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Keywords: | defer qs | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
While trying to get defer() to work with GeoQuerySets I ran into some strange issues. I eventually figured out that that problem was not isolated to GeoDjango. Here a toy model that demonstrates the problem:
from django.db import models
class City(models.Model):
name = models.CharField(max_length=30)
def __unicode__(self):
return self.name
Creating two cities, and showing what happens when the QuerySet is evaluated:
>>> c = City.objects.create(name='Pueblo') >>> c = City.objects.create(name='Houston') >>> City.objects.all() [<City: Pueblo>, <City: Houston>]
However, when evaluating a QuerySet with the name field deferred, this is what's returned:
>>> City.objects.defer('name')
[<City_Deferred_name: Pueblo>, <City_Deferred_name: Pueblo>]
Notice how it shows "Pueblo" for both records, when the second one should be "Houston." The same thing happens with slicing:
>>> qs = City.objects.defer('name')
>>> qs[0].name
u'Pueblo'
>>> qs[1].name
u'Pueblo'
My gut tells me that when DeferredAttribute is cached it is somehow cached for all of the other attributes. However, I cannot pinpoint how and/or where this is happening.
Attachments (8)
Change History (14)
comment:1 by , 17 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
by , 17 years ago
| Attachment: | defer.diff added |
|---|
comment:2 by , 17 years ago
| Has patch: | set |
|---|
comment:3 by , 17 years ago
by , 17 years ago
| Attachment: | defer_v2.diff added |
|---|
by , 17 years ago
| Attachment: | defer_v3.diff added |
|---|
by , 17 years ago
| Attachment: | defer.2.diff added |
|---|
add tests and code to make it work properly with foreing keys
by , 17 years ago
| Attachment: | defer.3.diff added |
|---|
by , 17 years ago
| Attachment: | defer.4.diff added |
|---|
comment:4 by , 17 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
I'm not convinced about this patch yet (Alex: work on getting one patch right, instead of submitting 14 really quick ones). All that instance.__dict__ stuff looks a bit ugly. But I see where you're going. Will work on it a bit over the weekend.
by , 17 years ago
| Attachment: | defer.5.diff added |
|---|
by , 17 years ago
| Attachment: | defer.6.diff added |
|---|
An alternate approach would be to put the pk in the class name, however that would mean creating O(n) classes, which strikes me as silly and wasteful of memory, given that this approach works.