Skip to content

Instantly share code, notes, and snippets.

@bluethon
Created November 25, 2016 10:01
Show Gist options
  • Save bluethon/80c7b909ac631509511d0b826192b261 to your computer and use it in GitHub Desktop.
Save bluethon/80c7b909ac631509511d0b826192b261 to your computer and use it in GitHub Desktop.
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)
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