ModelChoiceField给出了“选择一个有效的选择”,用ajax调用填充select

时间:2017-10-28 18:57:34

标签: python ajax django django-forms

我已经在多个线程上尝试了所有可能的解决方案,但我仍然无法解决问题。我有以下代码:

models.py

class CustomerVisit(models.Model):
  start_date = models.DateField()
  end_date = models.DateField()
  customer = models.ForeignKey(Customer)
  address = models.ForeignKey(Address)

forms.py

address = forms.ModelChoiceField(label='Address',
                                 queryset=Address.objects.none(),
                             widget=forms.Select(attrs={'style': 'width: 100%;'}))
customer = forms.ModelChoiceField(label='Customer',
                                  queryset=Customer.objects.all(),
                          widget=forms.Select(attrs={'style': 'width: 100%;'}))

views.py

if request.method == "POST":
    # Cleaning fields
    post = request.POST.copy()
    post['address'] = Address.objects.get(id=post['address'])
    post['start_date'] = dateparser.parse(post['start_date'])
    post['end_date'] = dateparser.parse(post['end_date'])
    # Updating request.POST
    request.POST = post
    form = CustomerVisitForm(request.POST)
    if form.is_valid():
        form.save(commit=True)
        return redirect("customervisit:calendar")

JS

$("#id_customer").select2({}).on("change", function () {
    var customer_id = $("#id_customer").val();
    var id_address = $("#id_address");
    id_address.select2({
        ajax: {
            url: '/get_customer_address/' + customer_id,
            dataType: "json",
            type: "GET",
            data: function (params) {

                var queryParameters = {
                    term: params.term
                }
                return queryParameters;
            },
            processResults: function (data) {
                return {
                    results: $.map(data, function (item) {
                        return {
                            text: item.text,
                            id: item.id
                        }
                    })
                };
            }
        }
    });
});

我的address选择使用ajax call using select2根据customer选项填充的内容。在阅读了几个主题后,我注意到modelchoicefield需要一个Address对象,这就是为什么我在验证表单之前在我的视图中使用以下代码的原因:{{1}但我仍然收到post['address'] = Address.objects.get(id=post['address'])错误

我使用Select a valid choice. That choice is not one of the available choices.,因为我需要一个空的选择

2 个答案:

答案 0 :(得分:2)

问题解决了。

如果将来有人遇到与我相同的错误,请查看我to_python保存的ModelChoiceField方法:

def to_python(self, value):
    if value in self.empty_values:
        return None
    try:
        key = self.to_field_name or 'pk'
        value = self.queryset.get(**{key: value})
    except (ValueError, TypeError, self.queryset.model.DoesNotExist):
        raise ValidationError(self.error_messages['invalid_choice'], code='invalid_choice')
    return value

所以我将queryset改为queryset=Address.objects而不是queryset=Address.objects.none()queryset=Address.objects.all()

感谢Daniel Roseman的评论

答案 1 :(得分:1)

我知道您要实现的目标,我的解决方案是使用小部件选择将字段初始化为charfield,然后覆盖clean方法。示例:

class InvoiceForm(forms.ModelForm):
    customer = forms.CharField(widget=forms.Select(attrs={'style': 'min-width: 150px;'}))

    class Meta:
        model = Invoice
        exclude = ['id', 'created', 'is_paid']

    def clean_customer(self):
        customer_id = self.cleaned_data['customer']
        customer = Customer.objects.get(id=customer_id)
        if customer: 
            return customer 
        else:
            raise ValidationError("Customer ID invalid")