Ticket #6953: 6953.2.diff

File 6953.2.diff, 8.7 KB (added by mk, 4 years ago)
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 8a48e15..9c1287f 100644
    a b def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_c 
    143143    field_list = []
    144144    ignored = []
    145145    opts = model._meta
    146     for f in opts.fields + opts.many_to_many:
     146    for f in sorted(opts.fields + opts.many_to_many):
    147147        if not f.editable:
    148148            continue
    149149        if fields is not None 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 9a962ad..6cd1a72 100644
    a b familiar with the mechanics. 
    371371OddForm is now an Article-related thing, because BadForm.Meta overrides
    372372CategoryForm.Meta.
    373373>>> OddForm.base_fields.keys()
    374 ['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories']
     374['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status']
    375375
    376376>>> class ArticleForm(ModelForm):
    377377...     class Meta:
    First class with a Meta class wins. 
    382382>>> class BadForm(ArticleForm, CategoryForm):
    383383...     pass
    384384>>> OddForm.base_fields.keys()
    385 ['headline', 'slug', 'pub_date', 'writer', 'article', 'status', 'categories']
     385['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status']
    386386
    387387Subclassing without specifying a Meta on the class will use the parent's Meta
    388388(or the first parent in the MRO if there are multiple parent classes).
    fields with the 'choices' attribute are represented by a ChoiceField. 
    556556<option value="...">Mike Royko</option>
    557557</select></td></tr>
    558558<tr><th>Article:</th><td><textarea rows="10" cols="40" name="article"></textarea></td></tr>
     559<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
     560<option value="...">Entertainment</option>
     561<option value="...">It&#39;s a test</option>
     562<option value="...">Third test</option>
     563</select><br /><span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>
    559564<tr><th>Status:</th><td><select name="status">
    560565<option value="" selected="selected">---------</option>
    561566<option value="1">Draft</option>
    562567<option value="2">Pending</option>
    563568<option value="3">Live</option>
    564569</select></td></tr>
    565 <tr><th>Categories:</th><td><select multiple="multiple" name="categories">
    566 <option value="...">Entertainment</option>
    567 <option value="...">It&#39;s a test</option>
    568 <option value="...">Third test</option>
    569 </select><br /><span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>
    570570
    571571You can restrict a form to a subset of the complete list of fields
    572572by providing a 'fields' argument. If you try to save a
    True 
    612612<option value="..." selected="selected">Mike Royko</option>
    613613</select></li>
    614614<li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
     615<li>Categories: <select multiple="multiple" name="categories">
     616<option value="...">Entertainment</option>
     617<option value="...">It&#39;s a test</option>
     618<option value="...">Third test</option>
     619</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    615620<li>Status: <select name="status">
    616621<option value="" selected="selected">---------</option>
    617622<option value="1">Draft</option>
    618623<option value="2">Pending</option>
    619624<option value="3">Live</option>
    620625</select></li>
    621 <li>Categories: <select multiple="multiple" name="categories">
    622 <option value="...">Entertainment</option>
    623 <option value="...">It&#39;s a test</option>
    624 <option value="...">Third test</option>
    625 </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    626626>>> f = TestArticleForm({'headline': u'Test headline', 'slug': 'test-headline', 'pub_date': u'1984-02-06', 'writer': unicode(w_royko.pk), 'article': 'Hello.'}, instance=art)
    627627>>> f.errors
    628628{}
    Add some categories and test the many-to-many form output. 
    675675<option value="..." selected="selected">Mike Royko</option>
    676676</select></li>
    677677<li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
     678<li>Categories: <select multiple="multiple" name="categories">
     679<option value="..." selected="selected">Entertainment</option>
     680<option value="...">It&#39;s a test</option>
     681<option value="...">Third test</option>
     682</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    678683<li>Status: <select name="status">
    679684<option value="" selected="selected">---------</option>
    680685<option value="1">Draft</option>
    681686<option value="2">Pending</option>
    682687<option value="3">Live</option>
    683688</select></li>
    684 <li>Categories: <select multiple="multiple" name="categories">
    685 <option value="..." selected="selected">Entertainment</option>
    686 <option value="...">It&#39;s a test</option>
    687 <option value="...">Third test</option>
    688 </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    689689
    690690Initial values can be provided for model forms
    691691>>> f = TestArticleForm(auto_id=False, initial={'headline': 'Your headline here', 'categories': [str(c1.id), str(c2.id)]})
    Initial values can be provided for model forms 
    699699<option value="...">Mike Royko</option>
    700700</select></li>
    701701<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
     702<li>Categories: <select multiple="multiple" name="categories">
     703<option value="..." selected="selected">Entertainment</option>
     704<option value="..." selected="selected">It&#39;s a test</option>
     705<option value="...">Third test</option>
     706</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    702707<li>Status: <select name="status">
    703708<option value="" selected="selected">---------</option>
    704709<option value="1">Draft</option>
    705710<option value="2">Pending</option>
    706711<option value="3">Live</option>
    707712</select></li>
    708 <li>Categories: <select multiple="multiple" name="categories">
    709 <option value="..." selected="selected">Entertainment</option>
    710 <option value="..." selected="selected">It&#39;s a test</option>
    711 <option value="...">Third test</option>
    712 </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    713713
    714714>>> f = TestArticleForm({'headline': u'New headline', 'slug': u'new-headline', 'pub_date': u'1988-01-04',
    715715...     'writer': unicode(w_royko.pk), 'article': u'Hello.', 'categories': [unicode(c1.id), unicode(c2.id)]}, instance=new_art)
    the data in the database when the form is instantiated. 
    818818<option value="...">Mike Royko</option>
    819819</select></li>
    820820<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
     821<li>Categories: <select multiple="multiple" name="categories">
     822<option value="...">Entertainment</option>
     823<option value="...">It&#39;s a test</option>
     824<option value="...">Third</option>
     825</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    821826<li>Status: <select name="status">
    822827<option value="" selected="selected">---------</option>
    823828<option value="1">Draft</option>
    824829<option value="2">Pending</option>
    825830<option value="3">Live</option>
    826831</select></li>
    827 <li>Categories: <select multiple="multiple" name="categories">
    828 <option value="...">Entertainment</option>
    829 <option value="...">It&#39;s a test</option>
    830 <option value="...">Third</option>
    831 </select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    832832>>> c4 = Category.objects.create(name='Fourth', url='4th')
    833833>>> c4
    834834<Category: Fourth>
    the data in the database when the form is instantiated. 
    845845<option value="...">Mike Royko</option>
    846846</select></li>
    847847<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
    848 <li>Status: <select name="status">
    849 <option value="" selected="selected">---------</option>
    850 <option value="1">Draft</option>
    851 <option value="2">Pending</option>
    852 <option value="3">Live</option>
    853 </select></li>
    854848<li>Categories: <select multiple="multiple" name="categories">
    855849<option value="...">Entertainment</option>
    856850<option value="...">It&#39;s a test</option>
    857851<option value="...">Third</option>
    858852<option value="...">Fourth</option>
    859853</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
     854<li>Status: <select name="status">
     855<option value="" selected="selected">---------</option>
     856<option value="1">Draft</option>
     857<option value="2">Pending</option>
     858<option value="3">Live</option>
     859</select></li>
    860860
    861861# ModelChoiceField ############################################################
    862862
Back to Top