[Answered ]-Django admin form custom select

1πŸ‘

βœ…

You can override the get_queryset method of the model admin class.

class CarAdmin(admin.ModelAdmin):
    list_display = ('manufacturer', 'model')
    exclude = ('owner',)  # note you need a comma to make this a tuple

    def get_queryset(self, request):
        qs = super(MyModelAdmin, self).get_queryset(request)
        return qs.objects.filter(owner__isnull=False)

Note it might not be a good idea to change the queryset like this β€” if you need to add an owner later on, you won’t be able to access those cars in the admin.

1πŸ‘

The ModelAdmin class has a .get_queryset() method, you could try writting your own queryset:

#admin.py
class CarAdmin(admin.ModelAdmin):
    list_display = ('manufacturer', 'model')
    exclude = ('owner')

    def get_queryset(self, request):
        return Car.objects.exclude(owner=None)

Leave a comment