[Fixed]-How to filter filter_horizontal in Django admin?

29๐Ÿ‘

โœ…

I found a solution by adapting the answer to a different question which I found in Google Groups

It works with a custom ModelForm like so:

Create a new forms.py:

from django import forms
from models import Equipment

class EquipmentModelForm(forms.ModelForm):
    class Meta:
        model = Equipment

    def __init__(self, *args, **kwargs):
        forms.ModelForm.__init__(self, *args, **kwargs)
        self.fields['equipment'].queryset = Equipment.avail.all()

Then in admin.py:

class SystemAdmin(admin.ModelAdmin):
    form = EquipmentModelForm
    filter_horizontal = ('equipment',) 

Hope this helps someone else out sometime.

๐Ÿ‘คnormic

5๐Ÿ‘

Old question, but anyway:

Depending on your specific requirements, it may be easier to set the limit_choices_to attribute on the equipment relation field. This works on ForeignKey fields as well as on ManyToManyFields.

According to the Django docs, this attribute

Sets a limit to the available choices for this field when this field
is rendered using a ModelForm or the admin โ€ฆ

A minimal example, assuming you have a System model with equipment many-to-many field:

class Equipment(models.Model):
    available = models.BooleanField(default=True)


class System(models.Model):
    equipment = models.ManyToManyField(to=Equipment,
                                       limit_choices_to={'available': True})


class SystemAdmin(admin.ModelAdmin):
    filter_horizontal = ['equipment']

This uses an available flag, but more complex queries can also be used.

๐Ÿ‘คdjvg

1๐Ÿ‘

The formfield_for_foreignkey method on a ModelAdmin handles this:
django docs

example using System and foreign key to Equipment:

def formfield_for_foreignkey(self, db_field, request, **kwargs):
    if db_field.name == 'equipment':
        system = System.objects.get(id=request.resolver_match.args[0])
        kwargs["queryset"] = system.equipment_set.all()
    return super().formfield_for_foreignkey(db_field, request, **kwargs)
๐Ÿ‘คSaleh Rabbaniy

Leave a comment