vendredi 30 avril 2021

check checkboxes for values that exists in a list

I am trying to check checkboxes that have values in a list of strings returned from a ModelForm class.

The field is declared in models.py file like this:

class IC(models.Model):
    class Meta:
        verbose_name = "Get Info"

    p_data = MultiSelectField("Some data", max_length=255, choices=choices.P_DATA_CHOICES, blank=True, null=True)

P_DATA_CHOICES looks like this

P_DATA_CHOICES = [
    ("ch1", "Choice 1"),
    ("ch2", "Choice 2"),
    ("ch3", "Choice 3"),
    ("ch4", "Choice 4"),
    ("ch5", "Choice 5"),
]

I have this ModelForm class in forms.py that I am implementing to the entire class:

class ICForm(ModelForm):
class Meta:
    model = IC
    exclude = ["fk_field"]


def __init__(self, *args, **kwargs):
    super(ICForm, self).__init__(*args, **kwargs)

    for i, f in self.fields.items():
        f.widget.attrs['class'] = "custom_class"

In my template I am generating the checkboxes as such:


If I check a few checkboxes (say Choice 1 and Choice 2) and submit the form, I can see ch1,ch2 in the p_data column of the database, but when I load the page again, their respective checkboxes are not checked.

I believe this has to do with this logic I'm trying to apply

Liquid error: Unknown operator in

I'm not sure if those values are sent from the ModelForm or not, and how I may access them in the template and loop through them.




Aucun commentaire:

Enregistrer un commentaire