2011-04-18 111 views

回答

2

limit_choices_to是你所追求的。

如果你只想在你的ModelAdmin有限的選擇,你應該相應地調整ModelForm

像這樣的東西應該這樣做:

class YourAdminForm(forms.ModelForm): 

    class Meta: 
     model = YourModel 

    def __init__(self, *args, **kwargs): 
     super(YourAdminForm, self).__init__(*args, **kwargs) 
     qs = self.fields['your_fk_field'].queryset 
     self.fields['your_fk_field'].queryset = qs.filter(is_active=1)