Django

Code

Changeset 4253

Show
Ignore:
Timestamp:
12/27/06 20:34:53 (2 years ago)
Author:
adrian
Message:

newforms: Implemented apply_changes() method for form_for_instance Forms

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/models.py

    r4250 r4253  
    2020        obj.save() 
    2121    return obj 
     22 
     23def make_apply_changes(opts, instance): 
     24    "Returns the apply_changes() method for a form_for_instance Form." 
     25    from django.db import models 
     26    def apply_changes(self, save=True): 
     27        if self.errors: 
     28            raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) 
     29        clean_data = self.clean_data 
     30        for f in opts.fields + opts.many_to_many: 
     31            if isinstance(f, models.AutoField): 
     32                continue 
     33            setattr(instance, f.attname, clean_data[f.name]) 
     34        if save: 
     35            instance.save() 
     36        return instance 
     37    return apply_changes 
    2238 
    2339def form_for_model(model, form=BaseForm): 
     
    4662    field_list = [] 
    4763    for f in opts.fields + opts.many_to_many: 
    48         current_value = getattr(instance, f.attname) 
     64        current_value = f.value_from_object(instance) 
    4965        formfield = f.formfield(initial=current_value) 
    5066        if formfield: 
    5167            field_list.append((f.name, formfield)) 
    5268    fields = SortedDictFromList(field_list) 
    53     return type(opts.object_name + 'InstanceForm', (form,), {'fields': fields, '_model': model}) 
     69    return type(opts.object_name + 'InstanceForm', (form,), 
     70        {'fields': fields, '_model': model, 'apply_changes': make_apply_changes(opts, instance)}) 
    5471 
    5572def form_for_fields(field_list): 
  • django/trunk/tests/modeltests/model_forms/models.py

    r4250 r4253  
    3131    pub_date = models.DateTimeField() 
    3232    writer = models.ForeignKey(Writer) 
    33     categories = models.ManyToManyField(Category
     33    categories = models.ManyToManyField(Category, blank=True
    3434 
    3535    def __str__(self): 
     
    3838__test__ = {'API_TESTS': """ 
    3939>>> from django.newforms import form_for_model, form_for_instance, BaseForm 
     40>>> import datetime 
    4041 
    4142>>> Category.objects.all() 
     
    143144hello 
    144145 
    145 Use form_for_instance to create a Form from a model instance. The difference 
    146 between this Form and one created via form_for_model is that the object's 
    147 current values are inserted as 'initial' data in each Field. 
     146Use form_for_instance to create a Form from a model instance. There are two 
     147differences between this Form and one created via form_for_model. First, the 
     148object's current values are inserted as 'initial' data in each Field. Second, 
     149the Form gets an apply_changes() method instead of a create() method. 
    148150>>> w = Writer.objects.get(name='Mike Royko') 
    149151>>> RoykoForm = form_for_instance(w) 
     
    151153>>> print f 
    152154<tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /></td></tr> 
     155 
     156>>> art = Article(headline='Test article', pub_date=datetime.date(1988, 1, 4), writer=w) 
     157>>> art.save() 
     158>>> art.id 
     1591 
     160>>> TestArticleForm = form_for_instance(art) 
     161>>> f = TestArticleForm(auto_id=False) 
     162>>> print f.as_ul() 
     163<li>Headline: <input type="text" name="headline" value="Test article" maxlength="50" /></li> 
     164<li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li> 
     165<li>Writer: <select name="writer"> 
     166<option value="">---------</option> 
     167<option value="1" selected="selected">Mike Royko</option> 
     168<option value="2">Bob Woodward</option> 
     169</select></li> 
     170<li>Categories: <select multiple="multiple" name="categories"> 
     171<option value="1">Entertainment</option> 
     172<option value="2">It&#39;s a test</option> 
     173<option value="3">Third test</option> 
     174</select></li> 
     175>>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', 'writer': u'1'}) 
     176>>> f.is_valid() 
     177True 
     178>>> new_art = f.apply_changes() 
     179>>> new_art.id 
     1801 
     181>>> new_art = Article.objects.get(id=1) 
     182>>> new_art.headline 
     183'New headline' 
    153184"""}