The model Station has a FK to System. I want to display Station count as a column in the System admin change list. I have achieved this with the code below but I am having difficulty making the 'num_stations' column sortable.
model.py:
class System(models.Model):
pass
class Station(models.Model):
system_info = models.ForeignKey(System)
admin.py:
class SystemAdmin(admin.ModelAdmin):
def num_stations(self, obj):
return obj.station_set.count()
# num_stations.admin_order_field = ????
num_stations.short_description = 'stations'
list_display = (num_stations',)
The ordinary 'station_set' syntax for a reverse relationship doesn't seem to work in the admin_order_field, even though other stack overflow question commonly show traversing a relation in the forward direction is supported e.g.