[Fixed]-Overriding queryset for inline admin

8👍

class MyInlineAdmin(admin.TabularInline):
    model = MyInlineModel
    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        """enable ordering drop-down alphabetically"""
        if db_field.name == 'car':
            kwargs['queryset'] = Car.objects.order_by("name") 
        return super(MyInlineAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

class ParentAdmin(admin.ModelAdmin):
    inlines = [MyInlineAdmin]

admin.site.register(ParentAdminModel, ParentAdmin)

Im assuming your models look something like:

class MyInlineModel(models.Model):
    car=models.Foreignkey(Car)
    #blah

for more on this; read the django Docs on formfield_for_foreignkey–>
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey

👤Komu

Leave a comment