简体   繁体   English

Django:Select 是一个有效的选择。 该选择不是可用的选择之一

[英]Django: Select a valid choice. That choice is not one of the available choices

I have a nested choice field: The top one is used to select a smartphone brand and the bottom one is used to select a smartphone model of the said brand.我有一个嵌套选择字段:顶部用于 select 智能手机品牌,底部用于 select 智能手机 model 该品牌。

The problem I face is such that when restricting the bottom choice using AJAX, my form is invalid.我面临的问题是,当使用 AJAX 限制底部选择时,我的表单无效。 However, the POST request with and without restriction are exactly the same:但是,有限制和无限制的 POST 请求是完全一样的:

No restriction:没有限制:

{'name': 'Ok iPhone 12 Mini', 'price': Decimal('345'), 'color': <Color: Red>, 'condition': <Condition: Refurbished by manufacturer>, 'storage': <StorageChoice: 128>, 'phone_model': <PhoneModel: iPhone 12 Mini>, 'description': '...', 'image': <InMemoryUploadedFile: pic.jpg (image/jpeg)>}

With restriction:有限制:

{'name': 'Ok iPhone 12 Mini', 'price': Decimal('345'), 'color': <Color: Red>, 'condition': <Condition: Refurbished by manufacturer>, 'storage': <StorageChoice: 128>, 'description': '...', 'image': <InMemoryUploadedFile: pic.jpg (image/jpeg)>, 'phone_model': <PhoneModel: iPhone 12 Mini>}

The only difference I can see is order, which should not matter in the case of a dictionary.我能看到的唯一区别是顺序,在字典的情况下这无关紧要。

views.py:视图.py:

def product_add(request):
    form = AddProductForm()
    if request.method == "POST":
        form = AddProductForm(request.POST, request.FILES)
        form.is_valid()
        form.cleaned_data['phone_model'] = PhoneModel.objects.get(id=request.POST['phone_model'])
        form.cleaned_data.pop('make', None)
        print(form.cleaned_data)
        if form.is_valid():
            form.cleaned_data['seller'] = request.user.customer
            Product.objects.create(**form.cleaned_data)
        else:
            print(form.errors)
    cart = get_cart(request)
    context = {'form': form, **cart}
    return render(request, 'store/product_add.html', context)

def load_models(request):
    make_id = request.GET.get('make')
    models = PhoneModel.objects.filter(phone_make=make_id)
    return render(request, 'part/product_add_model_options.html', {'models': models})

forms.py: forms.py:

class AddProductForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = ['name', 'price', 'color', 'condition',
                  'storage', 'phone_model', 'description', 'image']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        make = forms.ModelChoiceField(
            queryset=PhoneMake.objects.all(), required=False)
        self.fields['make'] = make
        self.fields['phone_model'].queryset = PhoneModel.objects.none()
        if 'make' in self.data:
            try:
                make_id = int(self.data.get('make'))
                self.fields['phone_model'].queryset = PhoneModel.objects.filter(
                    phone_make__phone_make=make_id)
            except Exception as e:
                print(e)

product.html (used with Jquery script to insert "options" into this "select"): product.html (与 Jquery 脚本一起使用以将“选项”插入此“选择”):

  <select name="phone_model" required id="id_model"> </select>

product_option.html: product_option.html:

<option value="">---------</option>
{% for model in models %}
<option value="{{ model.id }}">{{ model }}</option>
{% endfor %}

init method in the AddProductForm has following query self.fields['phone_model'].queryset = PhoneModel.objects.filter(phone_make__phone_make=make_id) it should be AddProductForm 中的init方法具有以下查询self.fields['phone_model'].queryset = PhoneModel.objects.filter(phone_make__phone_make=make_id)它应该是

self.fields['phone_model'].queryset = PhoneModel.objects.filter(
                phone_make=make_id)

, "phone_make" field is mentioned twice in your filter ,“phone_make”字段在您的过滤器中被提及两次

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择一个有效的选项。 该选择不是可用的选择之一——Django 表单 - Select a valid choice. That choice is not one of the available choices --Django forms Select 是一个有效的选择。 ...不是可用的选择之一 - Select a valid choice. ... is not one of the available choices 选择一个有效的选项。 [&quot;objects&quot;] 不是可用的选择之一。 在姜戈 - Select a valid choice. ["objects"] is not one of the available choices. in Django Django表单错误:选择一个有效的选项。 ......不是可用的选择之一 - Django Form Error: Select a valid choice. … is not one of the available choices ModelMultipleChoiceField CheckboxSelectMultiple选择一个有效的选择。 该选择不是可用的选择之一 - ModelMultipleChoiceField CheckboxSelectMultiple Select a valid choice. That choice is not one of the available choices 错误:Select 是一个有效的选择。 该选择不是可用的选择之一 - Error : Select a valid choice. That choice is not one of the available choices django modelformset_factory 引发表单无效:id 选择一个有效的选择。 该选择不是可用的选择之一 - django modelformset_factory raises form not valid: id Select a valid choice. That choice is not one of the available choices 选择一个有效的选项。 该选择不是可用的选择之一。 Django 表单出错 - Select a valid choice. That choice is not one of the available choices. error with Django Form Django表单:“选择一个有效的选择。 该选择不是可用的选择之一。” - Django Form: “Select a valid choice. That choice is not one of the available choices.” Django 管理员说:选择一个有效的选择。 该选择不是可用的选择之一 - Django admin says: Select a valid choice. That choice is not one of the available choices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM