Queryset "defer" won't work properly with select_related
|Reported by:||ruosteinen||Owned by:||ruosteinen|
|Component:||Database layer (models, ORM)||Version:||1.2-beta|
|Severity:||Keywords:||select_related, defer, only, nesting, query|
|Cc:||Carl Meyer, ruosteinen, Gregor Müllegger||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Select_related and defer don't seem to work properly together. Consider following models in APP buggy.
from django.db import models class ProblemModel(models.Model): name = models.CharField(max_length=100) target = models.ForeignKey('Target', null=True) class Target(models.Model): location = models.CharField(max_length=100)
Even though Queryset API document the use of underscore in deferring, the following ends up in assertion error.
from buggy.models import Target, ProblemModel Target.objects.all().delete(); ProblemModel.objects.all().delete() tgt = Target.objects.create(location='Earth') pm = ProblemModel.objects.create(name='bug', target=tgt) pm = ProblemModel.objects.select_related('target').defer('target__location').get(name='bug') assert pm.target.location == 'bug'
At least according to Queryset API docs, defer() is supposed to allow access to deferred fields even if they're not loaded initially.
In addition pm.target is not of type Target_Deferred_location_deferred as it should be(?). There seem to be some other weirdness as well with select_related and defer but they'll probably be fixed once nesting works properly.
This in Django 1.2 beta 1 SVN-12416
I'll start working on a patch right away.
Change History (16)
comment:1 Changed 7 years ago by
|Patch needs improvement:||unset|
comment:6 Changed 7 years ago by
|Owner:||changed from nobody to ruosteinen|
|Status:||new → assigned|
comment:9 Changed 7 years ago by
|Cc:||Gregor Müllegger added|
|Status:||closed → reopened|