Django

Code

Changeset 4299

Show
Ignore:
Timestamp:
01/08/07 23:22:48 (2 years ago)
Author:
adrian
Message:

newforms: Changed model auto-Form generation so that create() and apply_changes() are now both called save() -- for the purposes of simplicity

Files:

Legend:

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

    r4265 r4299  
    88__all__ = ('form_for_model', 'form_for_instance', 'form_for_fields') 
    99 
    10 def create(self, save=True): 
     10def model_save(self, commit=True): 
    1111    """ 
    1212    Creates and returns model instance according to self.clean_data. 
     
    1717        raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name) 
    1818    obj = self._model(**self.clean_data) 
    19     if save
     19    if commit
    2020        obj.save() 
    2121    return obj 
    2222 
    23 def make_apply_changes(opts, instance): 
    24     "Returns the apply_changes() method for a form_for_instance Form." 
     23def make_instance_save(opts, instance): 
     24    "Returns the save() method for a form_for_instance Form." 
    2525    from django.db import models 
    26     def apply_changes(self, save=True): 
     26    def apply_changes(self, commit=True): 
    2727        if self.errors: 
    2828            raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) 
     
    3232                continue 
    3333            setattr(instance, f.attname, clean_data[f.name]) 
    34         if save
     34        if commit
    3535            instance.save() 
    3636        return instance 
     
    5050            field_list.append((f.name, formfield)) 
    5151    fields = SortedDictFromList(field_list) 
    52     return type(opts.object_name + 'Form', (form,), {'fields': fields, '_model': model, 'create': create}) 
     52    return type(opts.object_name + 'Form', (form,), {'fields': fields, '_model': model, 'save': model_save}) 
    5353 
    5454def form_for_instance(instance, form=BaseForm): 
     
    6868    fields = SortedDictFromList(field_list) 
    6969    return type(opts.object_name + 'InstanceForm', (form,), 
    70         {'fields': fields, '_model': model, 'apply_changes': make_apply_changes(opts, instance)}) 
     70        {'fields': fields, '_model': model, 'save': make_instance_save(opts, instance)}) 
    7171 
    7272def form_for_fields(field_list): 
  • django/trunk/tests/modeltests/model_forms/models.py

    r4294 r4299  
    77The function django.newforms.form_for_model() takes a model class and returns 
    88a Form that is tied to the model. This Form works just like any other Form, 
    9 with one additional method: create(). The create() method creates an instance 
     9with one additional method: save(). The save() method creates an instance 
    1010of the model and returns that newly created instance. It saves the instance to 
    11 the database if create(save=True), which is default. If you pass 
    12 create(save=False), then you'll get the object without saving it. 
     11the database if save(commit=True), which is default. If you pass 
     12commit=False, then you'll get the object without committing the changes to the 
     13database. 
    1314 
    1415The function django.newforms.form_for_instance() takes a model instance and 
    1516returns a Form that is tied to the instance. This form works just like any 
    16 other Form, with one additional method: apply_changes(). The apply_changes() 
     17other Form, with one additional method: save(). The save() 
    1718method updates the model instance. It saves the changes to the database if 
    18 apply_changes(save=True), which is default. If you pass save=False, then you'll 
    19 get the object without saving it
     19save(commit=True), which is default. If you pass commit=False, then you'll 
     20get the object without committing the changes to the database
    2021""" 
    2122 
     
    7273>>> f.clean_data 
    7374{'url': u'entertainment', 'name': u'Entertainment'} 
    74 >>> obj = f.create() 
     75>>> obj = f.save() 
    7576>>> obj 
    7677<Category: Entertainment> 
     
    8384>>> f.clean_data 
    8485{'url': u'test', 'name': u"It's a test"} 
    85 >>> obj = f.create() 
     86>>> obj = f.save() 
    8687>>> obj 
    8788<Category: It's a test> 
     
    8990[<Category: Entertainment>, <Category: It's a test>] 
    9091 
    91 If you call create() with save=False, then it will return an object that hasn't 
    92 yet been saved. In this case, it's up to you to save it. 
     92If you call save() with commit=False, then it will return an object that 
     93hasn't yet been saved to the database. In this case, it's up to you to call 
     94save() on the resulting model instance. 
    9395>>> f = CategoryForm({'name': 'Third test', 'url': 'third'}) 
    9496>>> f.is_valid() 
     
    9698>>> f.clean_data 
    9799{'url': u'third', 'name': u'Third test'} 
    98 >>> obj = f.create(save=False) 
     100>>> obj = f.save(commit=False) 
    99101>>> obj 
    100102<Category: Third test> 
     
    105107[<Category: Entertainment>, <Category: It's a test>, <Category: Third test>] 
    106108 
    107 If you call create() with invalid data, you'll get a ValueError. 
     109If you call save() with invalid data, you'll get a ValueError. 
    108110>>> f = CategoryForm({'name': '', 'url': 'foo'}) 
    109111>>> f.errors 
     
    113115... 
    114116AttributeError: 'CategoryForm' object has no attribute 'clean_data' 
    115 >>> f.create() 
     117>>> f.save() 
    116118Traceback (most recent call last): 
    117119... 
    118120ValueError: The Category could not be created because the data didn't validate. 
    119121>>> f = CategoryForm({'name': '', 'url': 'foo'}) 
    120 >>> f.create() 
     122>>> f.save() 
    121123Traceback (most recent call last): 
    122124... 
     
    157159hello 
    158160 
    159 Use form_for_instance to create a Form from a model instance. There are two 
    160 differences between this Form and one created via form_for_model. First, the 
    161 object's current values are inserted as 'initial' data in each Field. Second, 
    162 the Form gets an apply_changes() method instead of a create() method. 
     161Use form_for_instance to create a Form from a model instance. The difference 
     162between this Form and one created via form_for_model is that the object's 
     163current values are inserted as 'initial' data in each Field. 
    163164>>> w = Writer.objects.get(name='Mike Royko') 
    164165>>> RoykoForm = form_for_instance(w) 
     
    189190>>> f.is_valid() 
    190191True 
    191 >>> new_art = f.apply_changes() 
     192>>> new_art = f.save() 
    192193>>> new_art.id 
    1931941