form.is_valid()对于Django Foms的选择选项字段返回false

时间:2019-06-02 12:04:00

标签: django django-models django-forms django-views

我正在使用Forms.Modelform创建一个表单。我的表单中有四个下拉字段(BLOCK_CODE,PANCHAYAT_CODE,OFFICER_DESIGNATION,ACTIVE_OR_NONACTIVE),这些字段通过在模型表单中通过视图传递的参数来进行过滤。直到这里一切正常。我的表单是用所需的字段和值创建的。现在,当我请求发布方法时,我的表单在验证时会失败。在命令窗口中打印表单(我已添加图片以供参考)后,我看到我的表单正在传递所有下拉文件(BLOCK_CODE,PANCHAYAT_CODE等)的下拉列表中的所有可用值,即使在下拉列表中选择了值也是如此。 由于我是django的新手,请帮助我所缺少的内容。我经历了很多关于stackoverflow可用的问题,但是找不到任何相关的解决方案。

--------------------- models.py -------

class OFFICER_INFORMATION(models.Model):
    NON_ACTIVE=0
    ACTIVE=1
    NONACTIVE_ACTIVE_choice = (
        (NON_ACTIVE, 'Non-Active'),
        (ACTIVE, 'Active'),
    )

    GRAM_ROZGAR_SEVAK = "GRS"
    TECHNICAL_OFFICER = "TO"
    DATA_OPERATOR = "DO"
    MONITORING_APO = "MAPO"
    WAGELIST_OPERATOR = "WG"
    FTO_GENERATION = "FTO"
    FIRST_FTO_SIGNATURE = "FSIGN"
    SECOND_FTO_SIGNATURE = "SSIGN"
    OFFICER_DESIGNATION_CHOICE = (
            (GRAM_ROZGAR_SEVAK,'GRAM ROZGAR SEVAK'),
            (TECHNICAL_OFFICER,'TECHNICAL OFFICER'),
            (DATA_OPERATOR,'DATA OPERATOR'),
            (MONITORING_APO,'MONITORING APO'),
            (WAGELIST_OPERATOR,'WAGELIST OPERATOR'),
            (FTO_GENERATION,'FTO GENERATION'),
            (FIRST_FTO_SIGNATURE,'FIRST FTO SIGNATURE'),
            (SECOND_FTO_SIGNATURE,'SECOND FTO SIGNATURE'),
        )
        BLOCK_CODE = models.ForeignKey(Block, on_delete=models.CASCADE)
        PANCHAYAT_CODE = models.ForeignKey(Panchayat, on_delete=models.CASCADE)
        OFFICER_NAME = models.CharField(max_length=255)
        OFFICER_DESIGNATION = models.CharField(max_length=100,choices=OFFICER_DESIGNATION_CHOICE)
        phone_regex = RegexValidator(regex=r'd{10}', message="Phone number must be entered in the format : '9999999999'. Up to 10 digits allowed.")
        OFFICER_MOBILE_CONTACT_NUMBER = models.CharField(validators=\[phone_regex\],max_length=10)
        ACTIVE_OR_NONACTIVE = models.BooleanField(choices=NONACTIVE_ACTIVE_choice)

        class Meta:
            db_table = "OFFICER_INFORMATION_TO_SEND_SMS"][1]][1]

''' --------------- views.py ------------------ '''

def officer_entry_create(request,id):
    BLOCK_CODE =int(id)
    data = dict()

    if request.method == 'POST':
        BLOCK_CODE = request.POST['BLOCK_CODE']
        form = OfficerForm(request.POST)
        print("form data request.post :",form)
        if form.is_valid():
            form.save()
            data['form_is_valid'] = True
        else:
            data['form_is_valid'] = False
    else:
        form = OfficerForm()

        context = {'form': form ,'BLOCK_CODE':BLOCK_CODE}
        data['html_form'] = render_to_string('sms_notification_app/includes/partial_officer_create.html',
            context,
            request=request
        )
    return JsonResponse(data)

'''

-------------- forms.py ---------------- '''

from .models import Panchayat,Block
class OfficerForm(forms.ModelForm):
    class Meta:
        model = OFFICER_INFORMATION
        fields = ('BLOCK_CODE', 'PANCHAYAT_CODE', 'OFFICER_DESIGNATION','OFFICER_NAME', 'OFFICER_MOBILE_CONTACT_NUMBER', 'ACTIVE_OR_NONACTIVE', )

    def __init__(self, BLOCK_CODE, *args, **kwargs):
        super(OfficerForm, self).__init__(*args, **kwargs)
        self.fields['BLOCK_CODE'].queryset = Block.objects.filter(BLOCK_CODE=BLOCK_CODE)
        self.fields['PANCHAYAT_CODE'].queryset = Panchayat.objects.filter(BLOCK_CODE_id=BLOCK_CODE).order_by('PANCHAYAT_NAME')

'''

我希望在发布请求时仅传递从下拉列表中选择的值

0 个答案:

没有答案