Django Admin SelectMultiple Widget

In my model, I have many different relationships between the two Users and Groups tables. In the admin interface, I see the SelectMultiple widget for groups. In fact, I use filter_horizontal, I see the available groups and the selected groups in two 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 groups in the group table. thank you

+3
source share
1 answer

In your form class, you can specify a user request for a group field, which then determines which group instances are available on 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
+3
source

Source: https://habr.com/ru/post/1704701/


All Articles