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

 Replying to [ticket:9150 erikcw]:
 > The form_class property for generic views such a
 django.views.generic.create_update.* should accept either a ModelForm
 class or a ModelForm object.  This would allow generic "wrapper views" to
 easily inject run-time data into the form instance.
 >
 > This will be useful for sites which use contrib.auth to create "members
 only" area.  Such sites would be able to use generic views while
 restricting the view's queryset to the currently logged in user.  It would
 also make it easier for the create_object generic view to set the "user
 ForeignKey" to the currently logged in user behind the scenes.  Right now,
 request.POST has to be manipulated in a wrapper view to hide this field
 from the end-user.
 >
 > If form_class would accept ModelForm(initial={'user': request.user,})
 instead of just ModelForm, this problem would be eased.  Alternatively,
 maybe generic views could accept another property containing a dictionary
 of desired arguments to feed to the ModelForm object.
 >
 > I think this or a similar enhancement would greatly expand the
 situations where generics can be used.

 Here is an example how it should be:

 models.py:
 {{{
 from django.db import models

 class Project(models.Model):
     name = models.CharField(max_length=200)
     auth_group = models.ForeignKey('auth.Group')
     def __unicode__(self):
         return self.name
 }}}

 forms.py:

 {{{
 from models import Project

 class ProjectForm(ModelForm):
     def __init__(self, request, *args, **kwargs):
         super(ProjectForm, self).__init__(*args, **kwargs)
         #set possible groups to the users groups
         self.fields['auth_group'].queryset = request.user.groups.all()
     class Meta:
         model = Project
 }}}

 views.py:
 {{{
 from forms import ProjectForm
 from django.views.generic import create_update
 def project_new(request):
     response = create_update.create_object(
         request,
         model = Project,
         form_class = ProjectForm(request),             # actually, only
 this is possible: form_class = ProjectForm
                                                        # but the
 ProjectForm requires one arguement (see forms.py)
         template_name = 'project_form.html',
     )
     return response

-- 
Ticket URL: <http://code.djangoproject.com/ticket/9150#comment:3>
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