django admin sort foreign key field list

后端 未结 3 1816
北海茫月
北海茫月 2021-01-30 17:12

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 woul

3条回答
  •  逝去的感伤
    2021-01-30 17:47

    It seems to work to add an admin class to the model admin, with the ordering equal to the field you want the drop down list sorted by.

    # in the admin.py file
    
    class SchoolAdmin(admin.ModelAdmin):
        ordering = ['school_name']
    
    admin.site.register(SchoolModel, SchoolAdmin)
    

    This works if you are willing to have an edit/add option next to drop down list.

提交回复
热议问题