Author: adrian
Date: 2006-12-26 23:15:22 -0600 (Tue, 26 Dec 2006)
New Revision: 4246

Modified:
  django/trunk/django/db/models/fields/related.py
  django/trunk/tests/modeltests/model_forms/models.py
Log:
newforms: Implemented formfield() for database ManyToManyField class and added 
unit tests

Modified: django/trunk/django/db/models/fields/related.py
===================================================================
--- django/trunk/django/db/models/fields/related.py     2006-12-27 05:14:34 UTC 
(rev 4245)
+++ django/trunk/django/db/models/fields/related.py     2006-12-27 05:15:22 UTC 
(rev 4246)
@@ -2,10 +2,12 @@
from django.db.models import signals, get_model
from django.db.models.fields import AutoField, Field, IntegerField, get_ul_class
from django.db.models.related import RelatedObject
+from django.utils.text import capfirst
from django.utils.translation import gettext_lazy, string_concat, ngettext
from django.utils.functional import curry
from django.core import validators
from django import oldforms
+from django import newforms as forms
from django.dispatch import dispatcher

# For Python 2.3
@@ -713,6 +715,9 @@
    def set_attributes_from_rel(self):
        pass

+    def formfield(self):
+        return forms.MultipleChoiceField(choices=self.get_choices_default(), 
required=not self.blank, label=capfirst(self.verbose_name))
+
class ManyToOneRel(object):
    def __init__(self, to, field_name, num_in_admin=3, min_num_in_admin=None,
        max_num_in_admin=None, num_extra_on_change=1, edit_inline=False,

Modified: django/trunk/tests/modeltests/model_forms/models.py
===================================================================
--- django/trunk/tests/modeltests/model_forms/models.py 2006-12-27 05:14:34 UTC 
(rev 4245)
+++ django/trunk/tests/modeltests/model_forms/models.py 2006-12-27 05:15:22 UTC 
(rev 4246)
@@ -101,6 +101,18 @@
...
ValueError: The Category could not be created because the data didn't validate.

+ManyToManyFields are represented by a MultipleChoiceField.
+>>> ArticleForm = form_for_model(Article)
+>>> f = ArticleForm(auto_id=False)
+>>> print f
+<tr><th>Headline:</th><td><input type="text" name="headline" maxlength="50" 
/></td></tr>
+<tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr>
+<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
+<option value="1">Entertainment</option>
+<option value="2">It&#39;s a test</option>
+<option value="3">Third test</option>
+</select></td></tr>
+
You can pass a custom Form class to form_for_model. Make sure it's a
subclass of BaseForm, not Form.
>>> class CustomForm(BaseForm):


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django 
updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to