checked CheckboxSelectMultiple check boxes

Hello!

I have model field called who_can_message, a JSONField model field which is supposed to store multiple user choices.

When I render the fields in the template, all the choices are available and they are all un-checked. In the database, I get a 5 item long (all available choices) list with the available choices.

The check boxes are un-checked when I refresh the page.

Can someone please explain why the check boxes are not checked after a refresh of the page?

class AccountSettings(models.Model):
    member = models.OneToOneField(Member, on_delete=models.CASCADE, blank=True, null=True)
    allow_messages = models.BooleanField(default=True)
    allow_comments = models.BooleanField(default=True)
    allow_room_reply = models.BooleanField(default=True)
    email_notifications = models.BooleanField(default=True)
    push_notifications = models.BooleanField(default=True)
    private_mode = models.BooleanField(default=False)
    who_can_message = models.JSONField(blank=True, default=who_can_message)
    
    class Meta:
        verbose_name = "Account setting"
    
    def __str__(self):
        return str(self.member)
def settingsview(request, username_slug):
    user = get_object_or_404(Member, username_slug=username_slug)
    user_settings = get_object_or_404(AccountSettings, member = user)
    
    if request.method == "POST":
        print(request.POST)
        form = SettingsForm(request.POST, instance=user_settings)
        if form.is_valid():
            form.save()
            messages.success(request, "Dina inställningar har sparats")
            return HttpResponseRedirect(reverse("settings_app:settings", kwargs={"username_slug": request.user.username_slug}))
    else:
        form = SettingsForm(instance=user_settings)

    context = {'form': form, "user_settings": user_settings}
    return render(request, 'settings_app/settings.html', context)
class SettingsForm(ModelForm):
    class Meta:
        model = AccountSettings
        exclude = ["member"]
        widgets = {
            "allow_messages": CheckboxInput(attrs={"class": "form-check-input", "id": "allow_messages", "name": "allow_messages"}),
            "allow_room_reply": CheckboxInput(attrs={"class": "form-check-input", "id": "allow_room_reply", "name": "allow_room_reply"}),
            "allow_comments": CheckboxInput(attrs={"class": "form-check-input", "id": "allow_comments", "name": "allow_comments"}),
            "email_notifications": CheckboxInput(attrs={"class": "form-check-input", "id": "email_notifications", "name": "email_notifications"}),
            "push_notifications": CheckboxInput(attrs={"class": "form-check-input", "id": "push_notifications", "name": "push_notifications"}),
            "private_mode": CheckboxInput(attrs={"class": "form-check-input", "id": "private_mode", "name": "private_mode"}),
            "who_can_message": CheckboxSelectMultiple(attrs={"class": "form-check-input", "id": "who_can_message", "name": "who_can_message"}, choices=profile_types),
        }
<div class="form-check form-switch" id="who_can_message_switches">
    <label class="form-check-label">Vem kan skicka meddelanden</label>
    <div class="checkbox-row">
        {% for checkbox in form.who_can_message %}
            {{checkbox}}
        {% endfor %}
    </div>
</div>

I managed to solve the issue.

Here is the answer:

<div class="checkbox-row">
    {% for checkbox in form.who_can_message %}
        <div>
            <input type="checkbox" name="who_can_message" value="{{checkbox.choice_label}}" class="form-check-input" id="who_can_message_{{forloop.counter0}}" {% if checkbox.choice_label in user_settings.who_can_message %}checked{% endif %}>
            <label for="who_can_message_{{ forloop.counter0 }}">{{checkbox.choice_label}}</label>
        </div>
    {% endfor %}
</div>