Limiting choices in a ModelAdmin list_filter

So, you need to limit choices, because the list of related choices is too long? Then do NOT use limit_choices_to parameter on the ForeignKey! Why? Because you risk having options missing in your forms, deleting relations unknowingly.

For instance, consider that you have the following case, expressed in pseudo code:

ModelClass.related_instance = models.ForeignKey(MyRelatedModel, limit_choices_to = {'active': True}, blank=True, null=True)  
object = <ModelClass instance>  
object.related_instance = <MyRelatedModel instance> = False

If you edit object, you will see an admin form where <MyRelatedModel instance> is not selectable! Hence, you will risk editing and loosing data without knowing it.

So your best bet is to stop using limit_choices_to and instead define a filter. Luckily, it's quite easy. It's not beautiful, though, since admin.filters.RelatedFieldListFilter does not offer a method to overwrite.

Here is an example:

class RelatedFilter(admin.filters.RelatedFieldListFilter):  
    def __init__(self, *args, **kwargs):  
        super(RelatedFilter, self).__init__(*args, **kwargs)  
        self.lookup_choices = [  
            (, x) for x in  

class MyModelAdmin(admin.ModelAdmin):

    list_filter = (('related_field', RelatedFilter), 'other_related_field')