views:

43

answers:

1

From my question at http://stackoverflow.com/questions/1362426/get-foreign-key-value, I managed to get the desired output...only one last bit remains. I want to sort my records by the year, make, then model in that order. I thought it'd be as simple as Vehicle.objects.all().order_by('common_vehicle') but this doesn't sort anything.

A: 

You have to order by specific fields in the related class. You do this by using the double-underscore format. So, for example:

Vehicle.objects.order_by('common_vehicle__year', 'common_vehicle__series__model__model')

to sort by the year value of the CommonVehicle class, then the model value of the Model class which is related via the Series class.

Note that this is a lot of joins, and could make your query performance quite slow. It may be fine for your needs, but just a heads-up that this is a potential source of slowness down the line.

Daniel Roseman
Didn't know about the double underscore. For now this'll have to do...I'll keep in mind to check the query performance. Thnx :)