Ticket #6953: modelform-m2m.diff

File modelform-m2m.diff, 7.4 KB (added by Alex Gaynor, 16 years ago)
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index e6bbb98..1c3b606 100644
    a b try:  
    1919    set
    2020except NameError:
    2121    from sets import Set as set     # Python 2.3 fallback
     22try:
     23    sorted
     24except NameError:
     25    from django.utils.itercompat import sorted
     26
    2227
    2328__all__ = (
    2429    'ModelForm', 'BaseModelForm', 'model_to_dict', 'fields_for_model',
    def fields_for_model(model, fields=None, exclude=None, formfield_callback=lambda  
    152157    # TODO: if fields is provided, it would be nice to return fields in that order
    153158    field_list = []
    154159    opts = model._meta
    155     for f in opts.fields + opts.many_to_many:
     160    for f in sorted(opts.fields + opts.many_to_many):
    156161        if not f.editable:
    157162            continue
    158163        if fields and not f.name in fields:
  • tests/modeltests/model_forms/models.py

    diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py
    index e12363c..44eaa56 100644
    a b familiar with the mechanics.  
    291291OddForm is now an Article-related thing, because BadForm.Meta overrides
    292292CategoryForm.Meta.
    293293>>> OddForm.base_fields.keys()
    294 ['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories']
     294['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status']
    295295
    296296>>> class ArticleForm(ModelForm):
    297297...     class Meta:
    First class with a Meta class wins.  
    302302>>> class BadForm(ArticleForm, CategoryForm):
    303303...     pass
    304304>>> OddForm.base_fields.keys()
    305 ['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories']
     305['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status']
    306306
    307307Subclassing without specifying a Meta on the class will use the parent's Meta
    308308(or the first parent in the MRO if there are multiple parent classes).
    fields with the 'choices' attribute are represented by a ChoiceField.  
    453453<option value="2">Bob Woodward</option>
    454454</select></td></tr>
    455455<tr><th>Article:</th><td><textarea rows="10" cols="40" name="article"></textarea></td></tr>
     456<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
     457<option value="1">Entertainment</option>
     458<option value="2">It&#39;s a test</option>
     459<option value="3">Third test</option>
     460</select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr>
    456461<tr><th>Status:</th><td><select name="status">
    457462<option value="" selected="selected">---------</option>
    458463<option value="1">Draft</option>
    459464<option value="2">Pending</option>
    460465<option value="3">Live</option>
    461466</select></td></tr>
    462 <tr><th>Categories:</th><td><select multiple="multiple" name="categories">
    463 <option value="1">Entertainment</option>
    464 <option value="2">It&#39;s a test</option>
    465 <option value="3">Third test</option>
    466 </select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr>
    467467
    468468You can restrict a form to a subset of the complete list of fields
    469469by providing a 'fields' argument. If you try to save a
    inserted as 'initial' data in each Field.  
    508508<option value="2">Bob Woodward</option>
    509509</select></li>
    510510<li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
     511<li>Categories: <select multiple="multiple" name="categories">
     512<option value="1">Entertainment</option>
     513<option value="2">It&#39;s a test</option>
     514<option value="3">Third test</option>
     515</select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
    511516<li>Status: <select name="status">
    512517<option value="" selected="selected">---------</option>
    513518<option value="1">Draft</option>
    514519<option value="2">Pending</option>
    515520<option value="3">Live</option>
    516521</select></li>
    517 <li>Categories: <select multiple="multiple" name="categories">
    518 <option value="1">Entertainment</option>
    519 <option value="2">It&#39;s a test</option>
    520 <option value="3">Third test</option>
    521 </select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
     522
    522523>>> f = TestArticleForm({'headline': u'Test headline', 'slug': 'test-headline', 'pub_date': u'1984-02-06', 'writer': u'1', 'article': 'Hello.'}, instance=art)
    523524>>> f.is_valid()
    524525True
    Add some categories and test the many-to-many form output.  
    569570<option value="2">Bob Woodward</option>
    570571</select></li>
    571572<li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
     573<li>Categories: <select multiple="multiple" name="categories">
     574<option value="1" selected="selected">Entertainment</option>
     575<option value="2">It&#39;s a test</option>
     576<option value="3">Third test</option>
     577</select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
    572578<li>Status: <select name="status">
    573579<option value="" selected="selected">---------</option>
    574580<option value="1">Draft</option>
    575581<option value="2">Pending</option>
    576582<option value="3">Live</option>
    577583</select></li>
    578 <li>Categories: <select multiple="multiple" name="categories">
    579 <option value="1" selected="selected">Entertainment</option>
    580 <option value="2">It&#39;s a test</option>
    581 <option value="3">Third test</option>
    582 </select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
    583584
    584585>>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04',
    585586...     'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']}, instance=new_art)
    the data in the database when the form is instantiated.  
    685686<option value="2">Bob Woodward</option>
    686687</select></li>
    687688<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
     689<li>Categories: <select multiple="multiple" name="categories">
     690<option value="1">Entertainment</option>
     691<option value="2">It&#39;s a test</option>
     692<option value="3">Third</option>
     693</select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
    688694<li>Status: <select name="status">
    689695<option value="" selected="selected">---------</option>
    690696<option value="1">Draft</option>
    691697<option value="2">Pending</option>
    692698<option value="3">Live</option>
    693699</select></li>
    694 <li>Categories: <select multiple="multiple" name="categories">
    695 <option value="1">Entertainment</option>
    696 <option value="2">It&#39;s a test</option>
    697 <option value="3">Third</option>
    698 </select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
    699700>>> Category.objects.create(name='Fourth', url='4th')
    700701<Category: Fourth>
    701702>>> Writer.objects.create(name='Carl Bernstein')
    the data in the database when the form is instantiated.  
    711712<option value="3">Carl Bernstein</option>
    712713</select></li>
    713714<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
    714 <li>Status: <select name="status">
    715 <option value="" selected="selected">---------</option>
    716 <option value="1">Draft</option>
    717 <option value="2">Pending</option>
    718 <option value="3">Live</option>
    719 </select></li>
    720715<li>Categories: <select multiple="multiple" name="categories">
    721716<option value="1">Entertainment</option>
    722717<option value="2">It&#39;s a test</option>
    723718<option value="3">Third</option>
    724719<option value="4">Fourth</option>
    725720</select>  Hold down "Control", or "Command" on a Mac, to select more than one.</li>
     721<li>Status: <select name="status">
     722<option value="" selected="selected">---------</option>
     723<option value="1">Draft</option>
     724<option value="2">Pending</option>
     725<option value="3">Live</option>
     726</select></li>
    726727
    727728# ModelChoiceField ############################################################
    728729
Back to Top