0

I would like not to display a field in form if I have a boolean field in database set to False.

Here is my code:

class CreateServer(ModelForm):
    def __init__(self, g, *args, **kwargs):
        super(CreateServer, self).__init__(*args, **kwargs)

        if g.boolean_clients:
            self.fields['clients'].queryset = Clients.objects.filter(game=g)
        else:
            # the fields['clients'] shouldn't be displayed in form
            pass

        ...

    class Meta:
        model = Server
        queryset = Server.objects.filter()
        fields = ['hostname', 'clients', 'map']

So if g.boolean_clients is true, there must be the filter, but if g.boolean_clients is false I do not want to display this field in form.

Is there any way hot to do it?

karthikr
  • 97,368
  • 26
  • 197
  • 188
Zoli
  • 831
  • 1
  • 11
  • 27

1 Answers1

0

I haven't tested this but try:

class CreateServer(ModelForm):
    def __init__(self, g, *args, **kwargs):
        super(CreateServer, self).__init__(*args, **kwargs)

        if g.boolean_clients:
            self.fields['clients'].queryset = Clients.objects.filter(game=g)
        else:
            self.fields.pop('clients')

    class Meta:
        model = Server
        queryset = Server.objects.filter()
        fields = ['hostname', 'clients', 'map']
crhodes
  • 1,178
  • 9
  • 20