views:

40

answers:

1

I need to offer a from in which a user can manage the permission associated to some Group.

I'd like to use the forms.ModelForm feature which comes from django, but I cannot understand how to modify the queryset over which the field cycles. I've also taken a deep look in contrib.admin and contrib.auth to discover where those forms are generated but cannot find it.

I'm trying not to use a normal modelForm so the precedent settings are already setted.

This is the scenario. I've added 40 (more or less) permissions to my project, all them codename starts with 'xxxxx.', so I'd like to do this:

class PermissionGroup(forms.ModelForm):
#permissions = forms.ModelMultipleChoiceField(queryset = Permission.objects.filter(codename__startswith = 'xxxxx.'), widget=forms.CheckboxSelectMultiple) 
class Meta:
    model = Group
    fields = ('permissions',)

How can I achieve the result? Or how can I bind precedent permission to a normal form.ModelForm?

Thanks in advance!

+1  A: 
class PermissionGroup(forms.ModelForm):
    permissions = forms.ModelMultipleChoiceField(Permission.objects.none(), widget=forms.CheckboxSelectMultiple)

    def __init__( self, pass_a_Q_object=None, *args, **kwargs ):
        super( PermissionGroup, self ).__init__( *args, **kwargs )
        if pass_a_Q_object:
            self.fields['permissions'].queryset = Permission.objects.filter(pass_a_Q_object) 
chefsmart
Thank you very much! It works as expected!
Enrico Carlesso