from django.forms import (ModelForm, CheckboxSelectMultiple,
                          MultipleChoiceField)

from .models import AuthorityElection, AuthorityElectionPosition


class AuthorityAreaForm(ModelForm):
    def __init__(self, user, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.user = user
        # import ipdb; ipdb.set_trace().
        self.fields['areas'] = MultipleChoiceField(
            choices=[
                (a.pk, a.name) for a in self.instance.authority.child_areas],
            label="Wards",
            widget=CheckboxSelectMultiple
        )

    class Meta:
        model = AuthorityElection
        fields = []

    def clean(self, *args, **kwargs):
        for area in self.cleaned_data['areas']:
            AuthorityElectionPosition.objects.get_or_create(
                authority_election=self.instance,
                user=self.user,
                area_id=area
            )
        return super().clean(*args, **kwargs)

@@ -693,32 +693,77 @@
 rgs, **kwargs):%0A
+        if 'areas' in self.cleaned_data:%0A    
         for area
@@ -794,16 +794,20 @@
 reas'%5D:%0A
+    
         
@@ -875,16 +875,20 @@
         
+    
 authorit
@@ -913,16 +913,20 @@
 stance,%0A
+    
         
@@ -965,16 +965,20 @@
         
+    
 area_id=
@@ -982,16 +982,20 @@
 id=area%0A
+    
         

Check that at least one area has been checked