如何在表单中保存Checbox数据

时间:2019-01-28 07:00:50

标签: django

  

如何以表格形式保存复选框数据,我只能保存以下药物   当我在药物名称之间使用空格时,一个字都没有错误   发生或未保存数据,以及每种药物的数量输入类型如何给出   为何不能保存中间带有空格的药品名称?

class Doctor_Patient_Med(models.Model):
            medicines=models.CharField(max_length=1000,blank=False)
            comments=models.TextField(max_length=1000,blank=False)
            follow_up=models.DateField(null=True,blank=True)
            patient=models.ForeignKey(Patient)
            doctor=models.ForeignKey(Doctor)

            def __str__(self):
                return self.patient.name

        class Medical_Meds(models.Model):

            medicines=models.CharField(max_length=250)
            amount=models.IntegerField(default=0)
            # is_purchase=models.BooleanField(default=False)
            p_date=models.DateTimeField(default=datetime.now)
            patient=models.ForeignKey(Patient)
            medical=models.ForeignKey(Medical)

      class Medical_Meds_Forms(forms.ModelForm):
        opt = []
        my = Doctor_Patient_Med.objects.all()
        for x in my:
            opt.append([x.medicines,x.medicines])
            total=len(opt)
            print(total)




    medicines =forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=opt)
        amount=forms.IntegerField(label='Amount',initial="0",required=True)
        p_date=forms.widgets.DateTimeInput(attrs={'type':'date'})
        # p_date = forms.DateTimeField(label='Purchase-Date', widget=forms.DateTimeInput(attrs={'type':'date'}))

    class Meta:
        model=Medical_Meds
        exclude=['patient','doctor','medical']
  

AJAX

$("#medical_form").click(function(){
    var matches = [];
    $(".selection-box:checked").each(function(){
        matches.push($(this).val());

    });


    $.ajax({
            url: createEvent,
            processData: false,
            contentType: false,
            type: 'POST',
            data:{
                  medicines:$('#medicines').val(),
                  amount:$('#amount').val(),
                  csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
                },
                success:function(){
                      alert("saved");

                    }

            }
        });
});
  

输出

why value doesn't consider both azithromycin tablets as one instead of value="azithromycin" tablet=""?

0 个答案:

没有答案