I'm looking for a way to use filter_horizontal on the base of a filtered queryset.
I've tried to use it with a custom manager:
In models.py:
class AvailEquipManager(models.Manager):
def get_query_set(self):
return super(AvailEquipManager, self).get_query_set().filter(id=3)
class Equipment(models.Model):
description = models.CharField(max_length=50)
manufacturer = models.ForeignKey(Manufacturer)
[...]
objects = models.Manager()
avail = AvailEquipManager()
def __unicode__(self):
return u"%s" % (self.description)
In admin.py:
class SystemAdmin(admin.ModelAdmin):
filter_horizontal = ('equipment',) # this works but obviously shows all entries
#filter_horizontal = ('avail',) # this does not work
So the questions is, how can I reduce the left side of the filter_horizontal to show only specific items?
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.
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 ManyToManyField
s.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With