Django

Code

Changeset 6915

Show
Ignore:
Timestamp:
12/12/07 20:48:04 (7 months ago)
Author:
jkocherhans
Message:

Fixed #6162. ModelForm?'s init signature now matches Form's. This is a backwards incompatbile change. Based largely on a patch by ubernostrum.

Files:

Legend:

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

    r6846 r6915  
    263263 
    264264class BaseModelForm(BaseForm): 
    265     def __init__(self, instance, data=None, files=None, auto_id='id_%s', prefix=None, 
    266                  initial=None, error_class=ErrorList, label_suffix=':'): 
    267         self.instance = instance 
     265    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, 
     266                 initial=None, error_class=ErrorList, label_suffix=':', instance=None): 
    268267        opts = self._meta 
    269         object_data = model_to_dict(instance, opts.fields, opts.exclude) 
     268        if instance is None: 
     269            # if we didn't get an instance, instantiate a new one 
     270            self.instance = opts.model() 
     271            object_data = {} 
     272        else: 
     273            self.instance = instance 
     274            object_data = model_to_dict(instance, opts.fields, opts.exclude) 
    270275        # if initial was provided, it should override the values from instance 
    271276        if initial is not None: 
  • django/trunk/docs/modelforms.txt

    r6863 r6915  
    2525 
    2626    # Creating a form to add an article. 
    27     >>> article = Article() 
    28     >>> form = ArticleForm(article) 
     27    >>> form = ArticleForm() 
    2928 
    3029    # Creating a form to change an existing article. 
    3130    >>> article = Article.objects.get(pk=1) 
    32     >>> form = ArticleForm(article) 
     31    >>> form = ArticleForm(instance=article) 
    3332 
    3433Field types 
     
    167166--------------------- 
    168167 
    169 Every form produced by ``ModelForm`` also has a ``save()`` method. This 
    170 method creates and saves a database object from the data bound to the form. 
    171 A subclass of ``ModelForm`` also requires a model instance as the first 
    172 arument to its constructor. For example:: 
     168Every form produced by ``ModelForm`` also has a ``save()`` 
     169method. This method creates and saves a database object from the data 
     170bound to the form. A subclass of ``ModelForm`` can accept an existing 
     171model instance as the keyword argument ``instance``; if this is 
     172supplied, ``save()`` will update that instance. If it's not supplied, 
     173``save()`` will create a new instance of the specified model:: 
    173174 
    174175    # Create a form instance from POST data. 
    175     >>> a = Article() 
    176     >>> f = ArticleForm(a, request.POST) 
     176    >>> f = ArticleForm(request.POST) 
    177177 
    178178    # Save a new Article object from the form's data. 
    179179    >>> new_article = f.save() 
     180 
     181    # Create a form to edit an existing Article. 
     182    >>> a = Article.objects.get(pk=1) 
     183    >>> f = ArticleForm(instance=a) 
    180184 
    181185Note that ``save()`` will raise a ``ValueError`` if the data in the form 
     
    202206 
    203207    # Create a form instance with POST data. 
    204     >>> a = Author() 
    205     >>> f = AuthorForm(a, request.POST) 
     208    >>> f = AuthorForm(request.POST) 
    206209 
    207210    # Create, but don't save the new author instance. 
     
    278281     
    279282        instance = Instance(required_field='value') 
    280         form = InstanceForm(instance, request.POST
     283        form = InstanceForm(request.POST, instance=instance
    281284        new_instance = form.save() 
    282285 
  • django/trunk/tests/modeltests/model_forms/models.py

    r6844 r6915  
    168168...     class Meta: 
    169169...         model = Category 
    170 >>> f = CategoryForm(Category()
     170>>> f = CategoryForm(
    171171>>> print f 
    172172<tr><th><label for="id_name">Name:</label></th><td><input id="id_name" type="text" name="name" maxlength="20" /></td></tr> 
     
    180180<input id="id_name" type="text" name="name" maxlength="20" /> 
    181181 
    182 >>> f = CategoryForm(Category(), auto_id=False) 
     182>>> f = CategoryForm(auto_id=False) 
    183183>>> print f.as_ul() 
    184184<li>Name: <input type="text" name="name" maxlength="20" /></li> 
     
    186186<li>The URL: <input type="text" name="url" maxlength="40" /></li> 
    187187 
    188 >>> f = CategoryForm(Category(), {'name': 'Entertainment', 'slug': 'entertainment', 'url': 'entertainment'}) 
     188>>> f = CategoryForm({'name': 'Entertainment', 'slug': 'entertainment', 'url': 'entertainment'}) 
    189189>>> f.is_valid() 
    190190True 
     
    197197[<Category: Entertainment>] 
    198198 
    199 >>> f = CategoryForm(Category(), {'name': "It's a test", 'slug': 'its-test', 'url': 'test'}) 
     199>>> f = CategoryForm({'name': "It's a test", 'slug': 'its-test', 'url': 'test'}) 
    200200>>> f.is_valid() 
    201201True 
     
    211211hasn't yet been saved to the database. In this case, it's up to you to call 
    212212save() on the resulting model instance. 
    213 >>> f = CategoryForm(Category(), {'name': 'Third test', 'slug': 'third-test', 'url': 'third'}) 
     213>>> f = CategoryForm({'name': 'Third test', 'slug': 'third-test', 'url': 'third'}) 
    214214>>> f.is_valid() 
    215215True 
     
    226226 
    227227If you call save() with invalid data, you'll get a ValueError. 
    228 >>> f = CategoryForm(Category(), {'name': '', 'slug': '', 'url': 'foo'}) 
     228>>> f = CategoryForm({'name': '', 'slug': '', 'url': 'foo'}) 
    229229>>> f.errors 
    230230{'name': [u'This field is required.'], 'slug': [u'This field is required.']} 
     
    237237... 
    238238ValueError: The Category could not be created because the data didn't validate. 
    239 >>> f = CategoryForm(Category(), {'name': '', 'slug': '', 'url': 'foo'}) 
     239>>> f = CategoryForm({'name': '', 'slug': '', 'url': 'foo'}) 
    240240>>> f.save() 
    241241Traceback (most recent call last): 
     
    254254...     class Meta: 
    255255...         model = Article 
    256 >>> f = ArticleForm(Article(), auto_id=False) 
     256>>> f = ArticleForm(auto_id=False) 
    257257>>> print f 
    258258<tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr> 
     
    287287...         model = Article 
    288288...         fields = ('headline','pub_date') 
    289 >>> f = PartialArticleForm(Article(), auto_id=False) 
     289>>> f = PartialArticleForm(auto_id=False) 
    290290>>> print f 
    291291<tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" /></td></tr> 
     
    299299...     class Meta: 
    300300...         model = Writer 
    301 >>> f = RoykoForm(w, auto_id=False
     301>>> f = RoykoForm(auto_id=False, instance=w
    302302>>> print f 
    303303<tr><th>Name:</th><td><input type="text" name="name" value="Mike Royko" maxlength="50" /><br />Use both first and last names.</td></tr> 
     
    310310...     class Meta: 
    311311...         model = Article 
    312 >>> f = TestArticleForm(art, auto_id=False
     312>>> f = TestArticleForm(auto_id=False, instance=art
    313313>>> print f.as_ul() 
    314314<li>Headline: <input type="text" name="headline" value="Test article" maxlength="50" /></li> 
     
    332332<option value="3">Third test</option> 
    333333</select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li> 
    334 >>> f = TestArticleForm(art, {'headline': u'Test headline', 'slug': 'test-headline', 'pub_date': u'1984-02-06', 'writer': u'1', 'article': 'Hello.'}
     334>>> f = TestArticleForm({'headline': u'Test headline', 'slug': 'test-headline', 'pub_date': u'1984-02-06', 'writer': u'1', 'article': 'Hello.'}, instance=art
    335335>>> f.is_valid() 
    336336True 
     
    348348...         model = Article 
    349349...         fields=('headline', 'slug', 'pub_date') 
    350 >>> f = PartialArticleForm(art, {'headline': u'New headline', 'slug': 'new-headline', 'pub_date': u'1988-01-04'}, auto_id=False
     350>>> f = PartialArticleForm({'headline': u'New headline', 'slug': 'new-headline', 'pub_date': u'1988-01-04'}, auto_id=False, instance=art
    351351>>> print f.as_ul() 
    352352<li>Headline: <input type="text" name="headline" value="New headline" maxlength="50" /></li> 
     
    371371...     class Meta: 
    372372...         model = Article 
    373 >>> f = TestArticleForm(new_art, auto_id=False
     373>>> f = TestArticleForm(auto_id=False, instance=new_art
    374374>>> print f.as_ul() 
    375375<li>Headline: <input type="text" name="headline" value="New headline" maxlength="50" /></li> 
     
    394394</select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li> 
    395395 
    396 >>> f = TestArticleForm(new_art, {'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04', 
    397 ...     'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']}
     396>>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04', 
     397...     'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']}, instance=new_art
    398398>>> new_art = f.save() 
    399399>>> new_art.id 
     
    404404 
    405405Now, submit form data with no categories. This deletes the existing categories. 
    406 >>> f = TestArticleForm(new_art, {'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04', 
    407 ...     'writer': u'1', 'article': u'Hello.'}
     406>>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04', 
     407...     'writer': u'1', 'article': u'Hello.'}, instance=new_art
    408408>>> new_art = f.save() 
    409409>>> new_art.id 
     
    417417...     class Meta: 
    418418...         model = Article 
    419 >>> f = ArticleForm(Article(), {'headline': u'The walrus was Paul', 'slug': u'walrus-was-paul', 'pub_date': u'1967-11-01', 
     419>>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': u'walrus-was-paul', 'pub_date': u'1967-11-01', 
    420420...     'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']}) 
    421421>>> new_art = f.save() 
     
    430430...     class Meta: 
    431431...         model = Article 
    432 >>> f = ArticleForm(Article(), {'headline': u'The walrus was Paul', 'slug': u'walrus-was-paul', 'pub_date': u'1967-11-01', 
     432>>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': u'walrus-was-paul', 'pub_date': u'1967-11-01', 
    433433...     'writer': u'1', 'article': u'Test.'}) 
    434434>>> new_art = f.save() 
     
    444444...     class Meta: 
    445445...         model = Article 
    446 >>> f = ArticleForm(Article(), {'headline': u'The walrus was Paul', 'slug': 'walrus-was-paul', 'pub_date': u'1967-11-01', 
     446>>> f = ArticleForm({'headline': u'The walrus was Paul', 'slug': 'walrus-was-paul', 'pub_date': u'1967-11-01', 
    447447...     'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']}) 
    448448>>> new_art = f.save(commit=False) 
     
    475475>>> cat.id 
    4764763 
    477 >>> form = ShortCategory(cat, {'name': 'Third', 'slug': 'third', 'url': '3rd'}
     477>>> form = ShortCategory({'name': 'Third', 'slug': 'third', 'url': '3rd'}, instance=cat
    478478>>> form.save() 
    479479<Category: Third> 
     
    487487...     class Meta: 
    488488...         model = Article 
    489 >>> f = ArticleForm(Article(), auto_id=False) 
     489>>> f = ArticleForm(auto_id=False) 
    490490>>> print f.as_ul() 
    491491<li>Headline: <input type="text" name="headline" maxlength="50" /></li> 
     
    691691...     class Meta: 
    692692...         model = PhoneNumber 
    693 >>> f = PhoneNumberForm(PhoneNumber(), {'phone': '(312) 555-1212', 'description': 'Assistance'}) 
     693>>> f = PhoneNumberForm({'phone': '(312) 555-1212', 'description': 'Assistance'}) 
    694694>>> f.is_valid() 
    695695True