views:

26

answers:

1

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?

+1  A: 

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