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

时间:2019-07-26 05:57:27

标签: django modelform cleaned-data

这里是情况:

我的模型如下:

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, )

然后我使用兴趣构建一个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')

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

2 个答案:

答案 0 :(得分:1)

观看次数

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

在比较条件下,您还可以将其用作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')

答案 1 :(得分:0)

非常轻微地更改了代码。看看是否有效。

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)