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 92. response = callback(request, *callback_args, **callback_kwargs) ファイル "C:\emodel_tracking..\emodel_tracking\tracker\views.py" in order_status 105. formset = OrderFormSet()

例外タイプ: TypeError at /accounts/profile/orderstatus/ 例外値: 'SearchOrderForm' オブジェクトは呼び出し可能ではありません

Django が呼び出し可能でないと言う原因となっている私の SearchOrderForm で私が間違っていることを誰かが知っていますか?

4

1 に答える 1

2

次のいずれかが必要だと思います。

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

formset = SearchOrderForm()
if formset.is_valid()

2 番目の方法が推奨される構文スタイルです。つまるところ、Django は Form 型とは異なる FormSet 型を提供しているため、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})
于 2010-03-12T20:48:19.510 に答える