#9200: Add a session based form wizard
-----------------------------------------------+----------------------------
          Reporter:  ddurham                   |         Owner:  ddurham        
     
            Status:  assigned                  |     Milestone:                 
     
         Component:  django.contrib.formtools  |       Version:  SVN            
     
        Resolution:                            |      Keywords:  session wizard 
forms
             Stage:  Accepted                  |     Has_patch:  1              
     
        Needs_docs:  0                         |   Needs_tests:  0              
     
Needs_better_patch:  0                         |  
-----------------------------------------------+----------------------------
Comment (by danaspiegel):

 I haven't had time to put together a patch (nor am I sure this is the best
 way to do this) but here's the code:

 {{{
     def GET(self, request, page_key):
         """
         Initialize a form if necessary, and display the form/page
 identified by
         page_key.
         """
         page_data = self._get_cleaned_data(request, page_key)
         if page_data is None:
             form = self._get_form_classes(request)[page_key]()
         else:
             form_class = self._get_form_classes(request)[page_key]
             if issubclass(form_class, forms.ModelForm):
                 # We need to manually create a new instance of the form's
 model, since we need to specially handle ManyToMany fields
                 instance = form_class.Meta.model()
                 for attribute_name, attribute_value in page_data.items():
                     if hasattr(instance, attribute_name):
                         setattr(instance, attribute_name, attribute_value)
                         if isinstance(attribute_value, base.Model):
                             # when the related object is a model object,
 we need to explicitly set the form field's value
                             page_data[attribute_name] = attribute_value.pk
                     elif isinstance(getattr(form_class.Meta.model,
 attribute_name), fields.related.ReverseSingleRelatedObjectDescriptor):
                         page_data[attribute_name] = attribute_value.pk
                     elif isinstance(getattr(form_class.Meta.model,
 attribute_name), fields.related.ReverseManyRelatedObjectsDescriptor):
                         page_data[attribute_name] = [value.pk for value in
 attribute_value]
                 form = form_class(instance=instance, initial=page_data)
             else:
                 form = form_class(initial=page_data)

         return self._show_form(request, page_key, form)

 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/9200#comment:25>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to