简体   繁体   中英

Django: dynamic ModelchoiceField not saving

I have the following 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',)

The CITY and ZIP is being filled dynamically by javascript and I am having a problem saving as form.is_valid is returning false and thus not saved unless I remove CITY and ZIP , what am I doing wrong?

my views

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

my 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>

Thanks in advance

In order to get True on calling form.is_valid(), city which the user have chosen must be a member of queryset CITY.objects.none(). Same thing is applicable for zip also. This is not possible.

Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM