select_related does not work in both directions..
|Reported by:||Justin||Owned by:||adrian|
|Component:||Database layer (models, ORM)||Version:|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
comparing the following two statements:
>>> for p in polls.get_list(select_related=True): print p, p.get_choice_list() >>> for c in choices.get_list(select_related=True): print c.get_poll(), c
The first statement runs one query per poll, while the second runs one query no matter how many polls you have.
This seems to be because the information about what models are related to each other does not get added to both ends of the relationship... now this is not 100% true because a poll obviously knows about what choices reference it...
My guess is that there needs to be a OneToMany class that compliments the ManyToOne relationship, letting the Poll class know that select_related should pull in choices.
Change History (3)
comment:2 Changed 10 years ago by Justin
- Resolution wontfix deleted
- Status changed from closed to reopened