#9150: Generic views should accept arguements for form_class
---------------------------------------------+------------------------------
          Reporter:  erikcw                  |         Owner:  toabctl          
                   
            Status:  assigned                |     Milestone:  1.1              
                   
         Component:  Generic views           |       Version:  1.0              
                   
        Resolution:                          |      Keywords:  generic views, 
form_class, arguments
             Stage:  Design decision needed  |     Has_patch:  1                
                   
        Needs_docs:  0                       |   Needs_tests:  0                
                   
Needs_better_patch:  0                       |  
---------------------------------------------+------------------------------
Changes (by toabctl):

  * has_patch:  0 => 1
  * milestone:  => 1.1

Comment:

 Attachment attachment:create_update.py.diff is a patch for this problem.

 = how the patch works =

 The Patch adds a new parameter for the create_object() method and
 update_object() method. the new parameter is called '''form_params'''
 (type is dict). it's now possible to pass parameters to the constructor of
 the form.

 = Example =

 == models.py ==
 {{{
 class Project(models.Model):
     """
     A Model which describe a Project
     """
     name = models.CharField(max_length=200)
         auth_group = models.ForeignKey('auth.Group')
     def __unicode__(self):
         return self.name
 }}}

 == forms.py ==
 {{{
 class ProjectForm(ModelForm):
     """
     Form for a project
     """
     def __init__(self, groups, *args, **kwargs):
         super(ProjectForm, self).__init__(*args, **kwargs)
         #set possible groups to the users groups
         self.fields['auth_group'].queryset = groups
     class Meta:
         model = Project
 }}}

 ProjectForm needs as parameter the variable '''groups'''. This is normally
 '''request.user.groups.all()'''.

 == views.py ==
 {{{
 def project_new(request):
     """
     A Form to create a new Project
     """
     response = create_update.create_object(
         request,
         form_class = ProjectForm,
         form_params = {'groups':request.user.groups.all(),},
         template_name = 'project_form.html',
     )
     return response

 def project_update(request, project_id):
     """
     A Form to Update a Project
     """
     response = create_update.update_object(
         request,
         form_class = ProjectForm,
         form_params = {'groups':request.user.groups.all(),},
         template_name = 'project_form.html',
         object_id = int(project_id),
         post_save_redirect = reverse('project-list')
     )
     #return response
 }}}

 in both methods, there is the var '''form_params''' passed.

 == project_form.html ==
 {{{
 {% if form %}
 <h1>{% trans "New Project" %}</h1>
         <form action="." method="POST">
         <table>
                 {{ form.as_table }}
         </table>
         <p><input type="submit" value="Submit"></p>
         </form>
 {% else %}

 {% endif %}
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/9150#comment:5>
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 [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-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to