这是我使用上述类的解决方案。我添加了许多过滤器以正确过滤它,但我想在此处使代码可读。
这正是我一直在寻找的东西,我在这里找到了解决方案:http : //www.slideshare.net/lincolnloop/customizing-the-django-admin#stats-bottom(幻灯片50)
将以下内容添加到我的admin.py中:
class CustomerForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(CustomerForm, self).__init__(*args, **kwargs) wtf = Category.objects.filter(pk=self.instance.cat_id); w = self.fields['categories'].widget choices = [] for choice in wtf: choices.append((choice.id, choice.name)) w.choices = choicesclass CustomerAdmin(admin.ModelAdmin): list_per_page = 100 ordering = ['submit_date',] # didnt have this one in the example, sorry search_fields = ['name', 'city',] filter_horizontal = ('categories',) form = CustomerForm这将过滤“类别”列表,而不会删除任何功能!(即:我仍然可以拥有我心爱的filter_horizontal :))
ModelForms非常强大,令我感到惊讶的是它没有在文档/书中详细介绍。



