Reputation: 25
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.
The problem I face is such that when restricting the bottom choice using AJAX, my form is invalid. However, the POST request with and without restriction are exactly the same:
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:
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:
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"):
<select name="phone_model" required id="id_model"> </select>
product_option.html:
<option value="">---------</option>
{% for model in models %}
<option value="{{ model.id }}">{{ model }}</option>
{% endfor %}
Upvotes: 0
Views: 1381
Reputation: 783
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
self.fields['phone_model'].queryset = PhoneModel.objects.filter(
phone_make=make_id)
, "phone_make" field is mentioned twice in your filter
Upvotes: 0