On Thu, 2007-10-11 at 17:34 -0700, Cat wrote:
> Hello
> 
> Can anyone tell me why when I get to the line - form =
> InstanceForm(request.POST) in the following view (stepping through the
> code), I get a Type Error SurveyForm object is not callable. My
> understanding is that it should be as it subclasses Form
> 
> def addEditSurvey(request, id = None):
> 
>       if id is not None:
>               instance = Survey.objects.get(id=id)
>               InstanceForm = SurveyForm(instance = instance)
>       else:
>               InstanceForm = SurveyForm()

This line (and the similar one two lines earlier) creates an instance of
the SurveyForm class. So InstanceForm isn't a class object, it's an
instance of a class.

> 
>       if request.method == 'POST':
>               form = InstanceForm(request.POST)

This line would only make sense if either (a) InstanceForm had a
__call__ method or (b) InstanceForm was a class object and so had a
constructor. Neither of these is true. You are writing code as if
InstanceForm was meant to be a class object -- and that is how the
Django examples all look -- but it isn't (see above).

If you want to create a class object that is based on things like the
"instance" parameter, you need to look at Python meta-programming. This
can be a fairly confusing area (because, by it's very nature, it's very
abstract). However, for simple cases, it's not too bad. Have a look at
django.newforms.models.form_for_model() for an example of creating a
class object based on parametrised input.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to