#9643 closed (invalid)
Custom managers on related objects generating incorrect SQL
Reported by: | Malcolm Tredinnick | Owned by: | Malcolm Tredinnick |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.0 |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Using these models, we can generate incorrect SQL.
class Report(models.Model): name = models.CharField(max_length=100) class TaskManager(models.Manager): def get_milestones(self): return super(TaskManager, self).get_query_set().filter(milestone=True) class Task(models.Model): report = models.ForeignKey(Report) name = models.CharField(max_length=100) milestone = models.BooleanField(default=False) objects = TaskManager() def __unicode__(self): return u"%s" % self.name
This gives the correct result:
>>> r = Report.objects.get(name="r1") >>> r.task_set.filter(milestone=True)
This does not (it doesn't filter on report_id
):
>>> r.task_set.get_milestones()
Whoopsie. :-(
Change History (3)
comment:1 by , 16 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 16 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
I'm an idiot. The bug is in the example code:
get_milestones()
should be callingself.get_query_set()
, not super'sget_query_set()
. This works finePEBKAC. :-(