2

Deprecated in Django 1.5: The depth parameter to select_related() has been deprecated. You should replace it with the use of the (*fields) listing specific related fields instead as documented above. A depth limit of relationships to follow can also be specified:

b = Book.objects.select_related(depth=1).get(id=4)
p = b.author         # Doesn't hit the database.
c = p.hometown 

how to replace iDjango 1.5 depth with the use of the (*fields)?

4

1 回答 1

0

in this specfic case

b = Book.objects.select_related("author", "hometown").get(id=4)

I suppose so many peoples get unused objects by using the depth parameter.

If you commonly use a given select_related with many fields, you may want to make a custom QuerySet to add it automaticaly with a custom method (something like qs.usually_related())

To add this kind of feature, you may want to follow this cool answer:

https://stackoverflow.com/a/2163921/267364

于 2013-03-07T08:40:54.903 回答