modelform“对象不可调用”错误

时间:2010-03-12 20:42:01

标签: django django-forms modelform

好的,所以我对Django很新,但是一直在阅读在线django书和djangoproject文档,但我似乎无法弄清楚这个错误:

我有'订单'型号:

class Orders(models.Model):
    client_id = models.ForeignKey(Client)
    order_date = models.DateField(auto_now_add = True)
    due_date = models.DateField()
    completion_date = models.DateField(blank=True, null=True)
    rush_order = models.BooleanField(default=False)
    billing_option = models.ForeignKey(Billing)
    patient_first_name = models.CharField(max_length=30)
    patient_middle_name = models.CharField(max_length=30, blank=True)
    patient_last_name = models.CharField(max_length=30)
    client_patient_id = models.CharField(max_length=30, blank=True)
    emodel_patient_id = models.CharField(max_length=30)
    special_instructions = models.TextField(blank=True)
    order_items = models.ManyToManyField(Order_Items)

    def __unicode__(self):
        return '%s : %s %s O: %s F: %s' % (self.client_id, self.patient_first_name, self.patient_last_name, self.order_date, self.completion_date)

    class Meta:
        ordering = ['client_id']

我有一个'SearchOrderForm'模型形式:


class SearchOrderForm(ModelForm):
    class Meta:
        model = Orders
        exclude = ('rush_order', 'billing_option', 'client_patient_id', 'special_instructions', 'order_items',)

我有'order_status'功能:

def order_status(request):
    error = False
    error_searching = False
    if request.method == 'POST':
            OrderFormSet = SearchOrderForm()
            formset = OrderFormSet()
            if formset.is_valid():
                cd = formset.cleaned_data()
                emodels_results = cd()
                emodels_results = cd(queryset = Order.objects.filter(Q(patient_first_name=search)|Q(patient_last_name=search)|Q(client_id=search)))
                patient_first_name = request.POST('patient_first_name', None)
                if patient_first_name:
                    emodels_results = emodels_results(queryset = Order.objects.filter(patient_first_name=patient_first_name))

                patient_last_name = request.POST('patient_last_name', None)
                if patient_last_name:
                    emodels_results = emodels_results(queryset = Order.objects.filter(patient_last_name=patient_last_name))

                client_id = request.POST('client_id', None)
                if client_id:
                    emodels_results = emodels_results(queryset = Order.objects.filter(client_id=client_id))

                return render_to_response('search_results.html', {'models': emodels_results})
            else:
                emodels_results = "Still messed up!"
                return render_to_response('search_results.html', {'models': emodels_results})
        else:
            error_searching = True
            form = SearchOrderForm()
            return render_to_response('order_status.html', {'form': form, 'error': error, 'error_searching': error_searching})

我可以毫无问题地填写表单,但是当我提交表单时,我会收到以下错误消息: 追溯: get_response中的文件“C:\ Python26 \ lib \ site-packages \ django \ core \ handlers \ base.py”   92. response = callback(request,* callback_args,** callback_kwargs) order_status中的文件“C:\ emodel_tracking .. \ emodel_tracking \ tracker \ views.py”   105. formset = OrderFormSet()

异常类型:/ accounts / profile / orderstatus /中的TypeError 异常值:'SearchOrderForm'对象不可调用

有没有人知道我的SearchOrderForm错误导致Django说它不可调用?

1 个答案:

答案 0 :(得分:2)

我想你想要其中任何一个:

OrderFormSet = SearchOrderForm()
if OrderFormSet.is_valid():

formset = SearchOrderForm()
if formset.is_valid()

第二种方式是首选的语法风格。作为挑剔的问题,Django提供的FormSet类型与Form类型不同,因此通常将Forms的实例称为“form”:

form = SearchOrderForm()
if form.is_valid():

您的代码会遇到其他一些问题:

def order_status(request):
    error = False
    error_searching = False
    if request.method == 'POST':
            #instead of:
            #OrderFormSet = SearchOrderForm()
            #formset = OrderFormSet()

            #instantiate an instance of your ModelForm
            #(I'd normally name it "form")
            formset = SearchOrderForm()


            if formset.is_valid():
                cd = formset.cleaned_data()
                #cd is now a Python dictionary
                #these next 2 lines don't make sense, what is your intention?
                emodels_results = cd()
                emodels_results = cd(queryset = Order.objects.filter(Q(patient_first_name=search)|Q(patient_last_name=search)|Q(client_id=search)))
                #you've already used your form to process and clean 
                #the incoming POST data. use the cleaned data instead
                #patient_first_name = request.POST('patient_first_name', None)
                patient_first_name = cd.get('patient_first_name','')

                #use data from the form's cleaned_data as in the line above
                #I'm not sure what your intention is with how the emodels_results
                #is but you'll need to rework that for it all to work 

                if patient_first_name:
                    emodels_results = emodels_results(queryset = Order.objects.filter(patient_first_name=patient_first_name))

                patient_last_name = request.POST('patient_last_name', None)
                if patient_last_name:
                    emodels_results = emodels_results(queryset = Order.objects.filter(patient_last_name=patient_last_name))

                client_id = request.POST('client_id', None)
                if client_id:
                    emodels_results = emodels_results(queryset = Order.objects.filter(client_id=client_id))

                return render_to_response('search_results.html', {'models': emodels_results})
            else:
                emodels_results = "Still messed up!"
                return render_to_response('search_results.html', {'models': emodels_results})
        else:
            error_searching = True
            form = SearchOrderForm()
            return render_to_response('order_status.html', {'form': form, 'error': error, 'error_searching': error_searching})