Django:动态ModelchoiceField没有保存

时间:2016-01-12 04:21:17

标签: python django django-forms django-views

我有以下Model ChoiceFIeld

class ad(ModelForm):

    REGION = forms.ModelChoiceField(queryset = REGION.objects.all())
    CITY = forms.ModelChoiceField(queryset = CITY.objects.all())
    ZIP = forms.ModelChoiceField(queryset = ZIP.objects.all())

class Meta:
    model = myinfo
    fields = ('REGION', 'CITY', 'ZIP',)

CITYZIP正在通过javascript动态填充,我遇到了问题,因为form.is_valid返回false并因此无法保存,除非我删除CITYZIP,我做错了什么?

我的观点

def index(request):
    city = CITY.objects.all()
    zip = ZIP.objects.all()

    form = ad(request.POST)

    if request.method == 'POST':


        if form.is_valid():
            form.save()

    else:
        form = ad()

    return render(request, 'dynamic/sample.html', {'adform':form, 'city': city, 'zip': zip,})

我的javascript

        <script>
        $(document).ready(function() {
            function removeOptions(selectbox)
            {
                var i;
                for(i=selectbox.options.length-1;i>=0;i--)
                {
                    selectbox.remove(i);
                }
            }
            removeOptions(document.getElementById("id_CITY"));
            removeOptions(document.getElementById("id_ZIP"));
            $("#id_CITY").append("<option value=\"\" selected=\"selected\">---------</option>");
            $("#id_ZIP").append("<option value=\"\" selected=\"selected\">---------</option>");
        });
    </script>

    <script>
        $(document).ready(function() {
            $("#id_REGION").change(function() {
                var el = $(this);
                function removeOptions(selectbox)
                {
                    var i;
                    for(i=selectbox.options.length-1;i>=0;i--)
                    {
                        selectbox.remove(i);
                    }
                }
                removeOptions(document.getElementById("id_CITY"));
                removeOptions(document.getElementById("id_ZIP"));
                $("#id_CITY").append("<option value=\"\" selected=\"selected\">---------</option>");
                $("#id_ZIP").append("<option value=\"\" selected=\"selected\">---------</option>");

                var reg = [{% for item in city %}"{{ item.reg_id }}"{% if not forloop.last %},{% endif %}{% endfor %}];
                var city_name = [{% for item in city %}"{{ item.name }}"{% if not forloop.last %},{% endif %}{% endfor %}];

                for(var i = 0; i<reg.length; i++){
                    if(el.val() == reg[i]){
                        $("#id_CITY").append("<option value = \"" + city_name[i] + "\">" + city_name[i] + "</option>");
                    }
                }
            });
        });
    </script>
    <script>
        $(document).ready(function() {
            $("#id_CITY").change(function() {
                var el = $(this);
                function removeOptions(selectbox)
                {
                    var i;
                    for(i=selectbox.options.length-1;i>=0;i--)
                    {
                        selectbox.remove(i);
                    }
                }
                removeOptions(document.getElementById("id_ZIP"));
                $("#id_ZIP").append("<option value=\"\" selected=\"selected\">---------</option>");

                var zip = [{% for item in zip %}"{{ item.cit }}"{% if not forloop.last %},{% endif %}{% endfor %}];
                var zip_num = [{% for item in zip %}"{{ item.num }}"{% if not forloop.last %},{% endif %}{% endfor %}];

                for(var i = 0; i<zip.length; i++){
                    if(el.val() == zip[i]){
                        $("#id_ZIP").append("<option value = \"" + zip_num[i] + "\">" + zip_num[i] + "</option>");
                    }
                }
            });
        });
    </script>

提前致谢

1 个答案:

答案 0 :(得分:0)

为了在调用form.is_valid()时获得True,用户选择的city必须是queryset CITY.objects.none()的成员。拉链也同样适用。这是不可能的。

尝试:

1. Put CITY.objects.all(), ZIP.objects.all() as querysets
2. Remove all options using js
2. Fill required options