Opened 10 years ago

Closed 9 years ago

Last modified 7 years ago

#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: UI/UX:


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" %

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 Changed 9 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:2 Changed 9 years ago by Malcolm Tredinnick

Resolution: invalid
Status: newclosed

I'm an idiot. The bug is in the example code: get_milestones() should be calling self.get_query_set(), not super's get_query_set(). This works fine

def get_milestones(self):
   return self.get_query_set().filter(milestone=True)


comment:3 Changed 7 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top