#5126: Populate `initial_data` of forms from model instance
------------------------------------------------+---------------------------
   Reporter:  Christopher Lenz <[EMAIL PROTECTED]>  |                Owner:  
adrian                
     Status:  new                               |            Component:  
django.newforms       
    Version:  SVN                               |           Resolution:         
               
   Keywords:                                    |                Stage:  Design 
decision needed
  Has_patch:  1                                 |           Needs_docs:  0      
               
Needs_tests:  1                                 |   Needs_better_patch:  0      
               
------------------------------------------------+---------------------------
Changes (by Christopher Lenz <[EMAIL PROTECTED]>):

  * summary:  Override constructor of `form_for_model` class to take model
              instance => Populate `initial_data` of forms
              from model instance

Comment:

 Replying to [comment:1 SmileyChris]:
 > I'm unconvinced on the merits of this ticket...
 
 Then let me try a bit harder ;-)
 
 Currently, if you don't use `form_for_instance`, there's simply no way in
 the API to use a model instance to populate the initial data of the
 form--short of workarounds such as the snippet you linked to. I think we
 can agree that this is a pretty common thing to want to do with newforms,
 and frankly, I'm puzzled by the omission.
 
 If there's no argument about the ''why'', let's move to the ''how''. I see
 three possible options to add this to the API, not necessarily mutually
 exclusive:
 
 1. A module-level function in `django.newforms.models` (see my second
 patch):
 {{{
 #!python
   form = MyForm()
   forms.init_from_instance(form, myinstance)
 }}}
 
 This is the only option I can see for custom `Form` classes, unless you
 want to add coupling between the regular forms code and the model-aware
 forms stuff, or add an intermediary `ModelForm` subclass or something like
 that.
 
 2. An added regular instance method on the `Form` subclass produced by
 `form_for_model()`
 
 {{{
 #!python
   form = MyForm()
   form.init(myinstance)
 }}}
 
 This is a bit nicer from an OO perspective.
 
 2. Finally, an overridden constructor as initially proposed by this
 ticket:
 
 {{{
 #!python
   form = MyForm(myinstance)
 }}}
 
 (A variant of this last form would to accept the model instance as value
 of the `initial` keyword argument.)
 
 IMHO it's obvious which of those is the nicest, but I'd buy any of them.
 
 To conclude, what I'd suggest is [attachment:ticket5126_2.diff] my second
 patch, which adds the `init_from_instance` function for use with custom
 `Form` classes, but still overrides the constructor
 .

-- 
Ticket URL: <http://code.djangoproject.com/ticket/5126#comment:2>
Django Code <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