Created
November 25, 2016 10:01
-
-
Save bluethon/80c7b909ac631509511d0b826192b261 to your computer and use it in GitHub Desktop.
From http://stackoverflow.com/questions/8992865/django-admin-sort-foreign-key-field-list
自定义admin中add下拉对象的顺序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def formfield_for_foreignkey(self, db_field, request, **kwargs): | |
if db_field.name == "section": | |
kwargs["queryset"] = self.model.section.field.related_model.objects.order_by('-pk') | |
return super(SectionExerciseAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment