select_related does not work in both directions..
|Reported by:||Justin||Owned by:||Adrian Holovaty|
|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.