Is there an option in the django admin view for ordering of foreign key fields? i.e. I have a foreign key to a "School" model, which shows as a dropdown, sorted on pk-- I would like it to be sorted alphabetically.
解决方案
Sure - you can...
ModelAdmin specific method: (the other methods are in my answer in the post linked to above)
class MyModelAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "school":
kwargs["queryset"] = School.objects.order_by('name')
return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
Examples for the other 3 non admin specific methods in my post linked above.