Problem with CheckboxSelectMultiple from a CharField ModelForm in Django

Viewed 61

I'm having trouble getting my form to save in Dajngo due to the following error on validation:

<ul class="errorlist"><li>pt_medical_condition<ul class="errorlist"><li>Select a valid choice.
 [&#x27;anx&#x27;, &#x27;Bip&#x27;] is not one of the available choices.
>pt_surgical_history<ul class="errorlist"><li>Select a valid choice. 
[&#x27;bre&#x27;, &#x27;pca&#x27;] is not one of the available choices.

I've got this model:

class pt_data(models.Model):
   condition_choices = [('ane', 'Anemia'), ('anx', 'Anxiety'), ('art', 'Arthritis'),
   ('ast', 'Asthma'), ('Bip', 'Bipolar'), ('ca', 'Cancer'), ('clo', 'Clotting disorder'),
   ('chf', 'CHF'), ('mdd', 'Depression'), ('cop', 'COPD'), ('ger', 'GERD'),
   ('gla', 'Glaucome'), ('hiv', 'HIV/AIDS'), ('ibs', 'IBS/Crohn\'s'),
   ('hld', 'High cholesterol'), ('ckd', 'Kidney disease'), ('ner', 'Nerve/Muscle disease'),
   ('ocd', 'OCD'), ('ost', 'Osteoporosis'), ('pai', 'Pain disorder'), ('pts', 'PTSD'),
   ('sch', 'Schizophrenia'), ('sei', 'Seizures'), ('sca', 'Sickle cell anemia'),
   ('su', 'Substance use disorder'), ('thy', 'Thyroid disease')]

   surgery_choices = [('app', 'Appendix removal'), ('bra', 'Brain surgery'),
   ('bre', 'Breast surgery'), ('cabg', 'CABG'), ('pca', 'Cardiac stent'),
   ('cho', 'Gallbladder removal'), ('col', 'Bowel surgery'), ('csec', 'C-section'),
   ('fra', 'Bone fracture repair'), ('her', 'Hernia repair'), ('hys', 'Uterus removal'),
   ('joi', 'Joint replacement'), ('lun', 'Lung surgery'), ('spi', 'Spine/back surgery'),
   ('thy', 'Thyroid surgery'), ('ton', 'Tonsil removal'), ('strf', 'Tubal ligation'),
   ('strm', 'Vasectomy'), ('wei', 'Weight reduction surgery')]


   pt_medical_condition = models.CharField(max_length=100, blank=True, null=True, 
   choices=condition_choices)
   pt_surgical_history = models.CharField(max_length=100, blank=True, null=True, choices=surgery_choices)

And this form:

class ptForm(forms.ModelForm):
   class Meta:
        model = pt_data
        fields = ('__all__')
        widgets = {
        'pt_medical_condition': CheckboxSelectMultiple(attrs={'class': 'cond_checkbox'}),
        'pt_surgical_history': CheckboxSelectMultiple(attrs={'class': 'surg_checkbox'}),
        }

And this is my HTML:

    {% for check in form.pt_medical_condition %}
        {% if forloop.counter|divisibleby:3 %}
            <div>            </div>
        {% endif %}
        <label class='mx-2' id="{{ check.choice_label }}">
            {{ check.tag }} {{ check.choice_label }}
        </label>
    {% endfor %}


    {% for check in form.pt_surgical_history %}
        {% if forloop.counter|divisibleby:2 %}
            <div>            </div>
        {% endif %}
        <label class='mx-2' id="{{ check.choice_label }}">
            {{ check.tag }} {{ check.choice_label }}
        </label>
    {% endfor %}

The HTML is rendered just fine, the problem seems to arise on submit. It looks like the correct characters are there, however they are surrounded by incorrect characters. The following "anx" is appropriate but won't be validatated by Django because everything else that has been tacked on &#x27;anx&#x27. Could it be a problem with posting the information with AJAX? Or maybe using a CSRF cookie to POST? Has anyone else encountered this problem? Any suggestions would be helpful.

0 Answers
Related