如何接收modelform_instance.cleaned_data['foreign key field']的视图?

how to receive modelform_instance.cleaned_data['foreign key field'] in view?

情况如下:

我有一个模型如下:

class Permit(Model):
        permit_option = BooleanField()

许可模型有两个对象:

Permit.objects.create(permit_option=False)  # id=1
Permit.objects.create(permit_option=True)  # id=2

我还有一个型号:

Interest(Model):
    permit_interest = ForeignKey(Permit, default=True, null=True, blank=True, on_delete=CASCADE, )

然后我使用 Interest 构建了一个 ModelForm:

class InterestForm(ModelForm):
    class Meta:
        model = Interest
        fields = '__all__'

我有一个看法:

def interest(request):
    template_name = 'interest_template.html'
    context = {}
    if request.POST:
        interest_form = InterestForm(request.POST)
        if interest_form.is_valid():
             if interest_form.cleaned_data['permit_interest'] == 2:
                return HttpResponse('True')
             elif interest_form.cleaned_data['permit_interest'] == 1:
                return HttpResponse('False')
             else:
                return HttpResponse('None')
     else:
        interest_form = InterestForm()
     context.update({interest_form': interest_form, })
     return render(request, template_name, context)

在 interest_template.html 我有:

<form method="post">
    {% csrf_token %}
    {{ interest_form.as_p }}
<button type="submit">Submit</button>
</form>

当我在表单字段中选择 True 并提交时,我希望看到 True。

或者当我在表单域中选择False并提交时看到False。

我尝试过的:

我测试了很多方法:

    if request.POST:
        interest_form = InterestForm(request.POST)
        if interest_form.is_valid():
             if interest_form.cleaned_data['permit_interest'] == True:
                return HttpResponse('True')
             elif interest_form.cleaned_data['permit_interest'] == False:
                return HttpResponse('False')
             else:
                return HttpResponse('None')

    if request.POST:
        interest_form = InterestForm(request.POST)
        if interest_form.is_valid():
             if interest_form.cleaned_data['permit_interest'] == 'True':
                return HttpResponse('True')
             elif interest_form.cleaned_data['permit_interest'] == 'False':
                return HttpResponse('False')
             else:
                return HttpResponse('None')

    if request.POST:
        interest_form = InterestForm(request.POST)
        if interest_form.is_valid():
             if interest_form.cleaned_data['permit_interest'] == '2':
                return HttpResponse('True')
             elif interest_form.cleaned_data['permit_interest'] == '1':
                return HttpResponse('False')
             else:
                return HttpResponse('None')

None 他们返回了我预期的行为,我似乎不明白这里发生了什么以及我必须做什么。

稍微更改了代码。看看能不能用。

def interest(request): 
    template_name = 'interest_template.html' 
    context = {} 
    if request.POST: 
        interest_form = InterestForm(request.POST) 
    if interest_form.is_valid(): 
        if interest_form.cleaned_data['permit_interest'] == 'true': 
            return HttpResponse('True') 
        elif interest_form.cleaned_data['permit_interest'] == 'false': 
            return HttpResponse('False') 
# rest of the code is unchanged

你可以试试这个。 您还可以尝试打印并查看清理后的表单为您带来的价值。

print(interest_form.cleaned_data['permit_interest)

观看次数

当您为外键执行清理方法时,他们将提供外键对象,因此您可以使用对象检查 permit_option 字段值,而 permit_option 是布尔字段,因此在 python 中比较条件需要给出布尔值,如 True 或 False

在比较条件下你也可以使用 as id

instance_Interest.id == 1:

    if request.POST:
        interest_form = InterestForm(request.POST)
        if interest_form.is_valid():
             instance_Interest = interest_form .cleaned_data['permit_interest']

             if instance_Interest:
                pass
             else:
                return HttpResponse('None')


             if instance_Interest.permit_option == True:
                return HttpResponse('True')
             elif instance_Interest.permit_option == False:
                return HttpResponse('False')
             else:
                return HttpResponse('None')