Wednesday, April 13, 2011

Django Admin SelectMultiple Widget

In my model I have many to many relationship between 2 tables Users and Groups. In the admin interface I see the SelectMultiple widget for Groups. Actually, I am using filter_horizontal, I see the available groups and the selected groups in 2 separate lists. Is it possible to filter the list of available groups that I can see (based on some criteria). I do not want to show all the groups in the groups table. Thank you

From stackoverflow
  • In your form class, you can specify a custom queryset for the group-field, which then determines which Group-instances are available in the form:

    class UserForm(forms.ModelForm):
        # override the default groups field
        groups = forms.ModelMultipleChoiceField(
            queryset=Groups.objects.filter(YOUR_CONDITIONS),
            widget=forms.SelectMultiple,
        )
    
        class Meta:
            model = User
    
  • How do I change the group widget that I see in the admin

0 comments:

Post a Comment