modelform“对象不可调用”错误

发布于 2024-08-24 22:29:18 字数 3422 浏览 7 评论 0原文

好的,所以我对 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. 响应 = 回调(请求,*callback_args,**callback_kwargs) order_status 中的文件“C:\emodel_tracking..\emodel_tracking\tracker\views.py” 105. formset = OrderFormSet()

异常类型:/accounts/profile/orderstatus/ 处的 TypeError 异常值:“SearchOrderForm”对象不可调用

有谁知道我的 SearchOrderForm 做错了什么,导致 Django 说它不可调用?

Ok, so I'm fairly new to Django, but have been reading both the online django book and the djangoproject documentation, but I can't seem to figure this error out:

I've got an 'Orders' model:

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']

I've got a 'SearchOrderForm' modelform:


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

and I've got an 'order_status' function:

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

I can fill out my form with no problems, but when I submit the form I get back the following error message:
Traceback:
File "C:\Python26\lib\site-packages\django\core\handlers\base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
File "C:\emodel_tracking..\emodel_tracking\tracker\views.py" in order_status
105. formset = OrderFormSet()

Exception Type: TypeError at /accounts/profile/orderstatus/
Exception Value: 'SearchOrderForm' object is not callable

Does anyone know what I'm doing wrong with my SearchOrderForm that's causing Django to say that it is not callable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2024-08-31 22:29:18

我认为您需要其中任何一种:

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

formset = SearchOrderForm()
if formset.is_valid()

第二种方式是首选语法样式。作为一个挑剔的问题,Django 提供了一个与 Form 类型不同的 FormSet 类型,因此约定将 Forms 的实例称为“表单”:

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

I think you want either of these:

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

formset = SearchOrderForm()
if formset.is_valid()

With the second way being the preferred syntax style. As a matter of nitpicking, Django offers a FormSet type which is different than the Form type so it is convention to refer to instances of Forms as "form":

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

You are going to have some other problems with your code:

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})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文