1

I use MultipleChoiceField in form. It shows me REQUIREMENTS_CHOICES list with checkboxes where user can select and add new requirements to database. Is it possible to disable checkboxes (in my case requirements) which is already in the database? Lets say I have A and C in database so I dont need them.

First value in tulpe is symbol of requirement, second value is the name of requirement.

models.py:

class Requirement(models.Model):
    code = models.UUIDField(_('Code'), primary_key=True, default=uuid.uuid4, editable=False)
    symbol = models.CharField(_('Symbol'), max_length=250)
    name = models.CharField(_('Name'), max_length=250)

forms.py:

REQUIREMENTS_CHOICES = (
        ('A', 'Name A'),
        ('B', 'Name B'),
        ('C', 'Name C'),
        ('D', 'Name D'),
)


class RequirementAddForm(forms.ModelForm):
    symbol = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, choices=REQUIREMENTS_CHOICES,)

    class Meta:
        model = Requirement
        fields = ('symbol',)

views.py:

def requirement_add(request):
    data = dict()
    if request.method == 'POST':
        form = RequirementAddForm(request.POST)
        if form.is_valid():
            list = dict(REQUIREMENTS_CHOICES) # {'C': 'Name C', 'A': 'Name A', 'B': 'Name B'}
            symbols = form.cleaned_data.get('symbol') # ['A', 'B', 'C']
            requirement = form.save(commit=False)
            for symbol in symbols:
                requirement.symbol = symbol
                requirement.name = list[symbol]
                requirement.save()
            data['form_is_valid'] = True
            requirements = Requirement.objects.filter()
            context = {requirement': requirement, 'requirements': requirements}
            data['html_requirement'] = render_to_string('project/requirement_list.html', context)
        else:
            data['form_is_valid'] = False
    else:
        form = RequirementAddForm()
    context = {'form': form}
    data['html_requirement_form'] = render_to_string('project/requirement_add.html', context, request=request)
    return JsonResponse(data)
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193

1 Answers1

1

You can manipulate your form at it's initialization:

REQUIREMENTS_CHOICES = (
        ('A', 'Name A'),
        ('B', 'Name B'),
        ('C', 'Name C'),
        ('D', 'Name D'),
)


class RequirementAddForm(forms.ModelForm):
    def __init__(self, symbols='', *args, **kwargs):
        super(RequirementAddForm, self).__init__(*args, **kwargs)

        UPDATED_CHOICES = () # empty tuple
        for choice in REQUIREMENTS_CHOICES:
            if choice[1] not in symbols:
                UPDATED_CHOICES += (choice,) # adds choice as a tuple

        self.fields['symbol'] = forms.MultipleChoiceField(
                                    required=False,
                                    widget=forms.CheckboxSelectMultiple, 
                                    choices=UPDATED_CHOICES,
                                )

    class Meta:
        model = Requirement

What happens above:

  1. When you initialize your form
    (ex: form=RequirementAddForm(symbols=existing_symbols)), you can pass to the constructor, a string with the existing symbols for an item in your database.
  2. The __init__ function checks which choices exist already in symbols and updates the UPDATED_CHOICES accordingly.
  3. A field named symbol gets added to the form from the constructor, which have as choices the UPDATED_CHOICES.
John Moutafis
  • 22,254
  • 11
  • 68
  • 112
  • Hello! I add my view which add new objects (in my case requirements) with selected data. Each object has 3 field: code, name, symbol. I am little bit comfused with this line of your code: `if choice[1] not in symbols`. Where and how I take symbols? For example in view I take selected symbols by this code: `symbols = form.cleaned_data.get('symbol') # ['A', 'B', 'C']` – Nurzhan Nogerbek Apr 25 '17 at 15:45
  • I am tring to change constructor: `form = RequirementAddForm(request.POST, symbols=symbols)` but it says that `symbols=symbols` not correct. What do you think about that?! – Nurzhan Nogerbek Apr 25 '17 at 16:35
  • You have to rearrange the `requirement_add` to fetch the existing `symbols` for a new requirement and then pass them to the constructor. – John Moutafis Apr 26 '17 at 09:38