0

I have form and model as below. I am using Django 1.6.4. I am able to create multiple choice field with defualt checked options based on parameter:

self.fields["options"].initial

but how to set this checked options as disabled to edit, when I set,

self.fields["options"].widget.attrs['disabled']

it made all fields as disabled. Is it possible to handle it in form, or should I use CSS styles for it.

class Option(models.Model):
    name = models.CharField(max_length=20, verbose_name="Name", unique=True)

class Device(models.Model):
    options = models.ManyToManyField(Option, null=True, blank=True)
    serial_number = models.CharField(max_length=20, verbose_name="Serial number", unique=True)

class EditDeviceForm(forms.Form):
      serial_number = forms.CharField(label="Serial number:")  
      options = forms.ModelMultipleChoiceField(label="Options:", 
                widget=forms.CheckboxSelectMultiple(),
                queryset=Option.objects.all()) 
      def __init__(self, *args, **kwargs):
                super(EditDeviceForm, self).__init__(*args, **kwargs)
                self.fields["options"].initial = Option.objects.filter(device__in = Device.objects.filter(serial_number=s_number))
                #self.fields["options"].widget.attrs['disabled'] = "disabled"
risque
  • 83
  • 1
  • 1
  • 6
  • duplicate of this question? http://stackoverflow.com/questions/324477/in-a-django-form-how-to-make-a-field-readonly-or-disabled-so-that-it-cannot-b – lukeaus Feb 13 '15 at 18:09
  • No, that question I assume is about how very carefully use disable option, It is also very helpful. I was interested in very specific usage. GwynBleidD was OK for me. – risque Feb 13 '15 at 21:02

1 Answers1

0

As for RadioSelect, you can iterate in template through all of your checkboxes and apply some specific data to it.

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77