尝试使用Django表单保存时,动态生成AJAX下拉菜单重置为原始状态吗? [英] Dynamically build AJAX dropdown resets to original state when trying to save with django form?

查看:88
本文介绍了尝试使用Django表单保存时,动态生成AJAX下拉菜单重置为原始状态吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有几个下拉列表与AJAX链接在一起.

I my form I have few dropdowns chained between them with AJAX.

这就是我填充它们的方式

This is how I a populating them

    function getleaseterm() {

        //get a reference to the select element
        $select = $('#id_leaseterm');
        //request the JSON data and parse into the select element
        var l_id = ($("select[name='lease'] option:selected").attr('value'));
        //var l_id = 13;
        l_url = "/api/get_leaseterm/"+l_id+"/";

        $.ajax({
          url: l_url,
          dataType:'JSON',
          success:function(data1){
            //clear the current content of the select
            $select.empty();
            $select.append('<option value="-1">Select term </option>');
            //iterate over the data and append a select option

            $.each(data1, function(key, val){
              $select.append('<option value="' + val.id + '">' + val.as_char + '</option>');
            })
          },

        });

}

这是控件

<select class="select" id="id_leaseterm" name="leaseterm">
<option value="-1" selected="selected">-----</option>
</select>

一切正常,我正在更改下拉菜单中的值,并且其他下拉菜单的选项也已更新.因此,我可以选择相关的值.

It all works , I am changing values in my dropdowns and options of other dropdowns are updated.So I can select the relevant value.

问题是,当我保存for时-表单获取的不是我在其中放置的值,而是在进行任何Ajax操作之前存在的默认值.

The problem is when I Save the for - the form gets not the value that I have put there but the default value that was there before any manipulation of Ajax have been done.

此外,当我查看源代码时,我在代码中看到的是默认值,而不是从我用AJAX构建的内容中选择的内容(即使在屏幕上,我也确实在选择选项中看到了正确的值...)

Also when I do view source I see in the code is default value and not what was selected from what I have build with AJAX.(Even that on the screen I do see the correct values in the select options...)

我的后端是Django. 我的Django表单采用以下格式设置.

My backend is Django . My Django form formatted in following way.

class MassPaymentForm(forms.ModelForm):

    leaseterm = forms.ModelChoiceField(queryset=LeaseTerm.objects.none()) # Need to populate this using jquery
    lease= forms.ModelChoiceField(queryset=Lease.objects.none()) # Need to populate this using jquery
    class Meta:
        model = LeasePayment

        fields = ['lease', 'leaseterm', 'payment_type', 'is_deposit',  'amount', 'method', 'payment_date', 'description'] 

可能是什么问题?还有解决的办法吗?

What could be the problem ? And any idea how to solve it?

推荐答案

我知道了问题所在 它在我的表单文件中

I have figured out what was the problem it is in my form file

如果我仅将租期和租期字段保留在Meta类中,则它不会在提交时重置那些字段.我所有的逻辑最终都按设计工作.

If I keep the lease and leaseterm fields just in Meta class that it doesn't reset those fields on submit any more . And all my logic finally works as designed.

class MassPaymentForm(forms.ModelForm):


    class Meta:
        model = LeasePayment

        fields = ['lease', 'leaseterm', 'payment_type', 'is_deposit',  'amount', 'method', 'payment_date', 'description']

这篇关于尝试使用Django表单保存时,动态生成AJAX下拉菜单重置为原始状态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆