Author: adrian
Date: 2006-12-26 23:23:21 -0600 (Tue, 26 Dec 2006)
New Revision: 4247
Modified:
django/trunk/django/db/models/fields/related.py
django/trunk/tests/modeltests/model_forms/models.py
Log:
newforms: Implemented formfield() for database ForeignKey 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:15:22 UTC
(rev 4246)
+++ django/trunk/django/db/models/fields/related.py 2006-12-27 05:23:21 UTC
(rev 4247)
@@ -361,7 +361,7 @@
old_ids = set([obj._get_pk_val() for obj in objs])
cursor = connection.cursor()
cursor.execute("DELETE FROM %s WHERE %s = %%s AND %s IN (%s)" %
\
- (self.join_table, source_col_name,
+ (self.join_table, source_col_name,
target_col_name, ",".join(['%s'] * len(old_ids))),
[self._pk_val] + list(old_ids))
transaction.commit_unless_managed()
@@ -548,6 +548,9 @@
def contribute_to_related_class(self, cls, related):
setattr(cls, related.get_accessor_name(),
ForeignRelatedObjectsDescriptor(related))
+ def formfield(self):
+ return forms.ChoiceField(choices=self.get_choices_default(),
required=not self.blank, label=capfirst(self.verbose_name))
+
class OneToOneField(RelatedField, IntegerField):
def __init__(self, to, to_field=None, **kwargs):
try:
Modified: django/trunk/tests/modeltests/model_forms/models.py
===================================================================
--- django/trunk/tests/modeltests/model_forms/models.py 2006-12-27 05:15:22 UTC
(rev 4246)
+++ django/trunk/tests/modeltests/model_forms/models.py 2006-12-27 05:23:21 UTC
(rev 4247)
@@ -20,9 +20,16 @@
def __str__(self):
return self.name
+class Writer(models.Model):
+ name = models.CharField(maxlength=50)
+
+ def __str__(self):
+ return self.name
+
class Article(models.Model):
headline = models.CharField(maxlength=50)
pub_date = models.DateTimeField()
+ writer = models.ForeignKey(Writer)
categories = models.ManyToManyField(Category)
def __str__(self):
@@ -101,12 +108,24 @@
...
ValueError: The Category could not be created because the data didn't validate.
-ManyToManyFields are represented by a MultipleChoiceField.
+Create a couple of Writers.
+>>> w = Writer(name='Mike Royko')
+>>> w.save()
+>>> w = Writer(name='Bob Woodward')
+>>> w.save()
+
+ManyToManyFields are represented by a MultipleChoiceField, and ForeignKeys are
+represented by a ChoiceField.
>>> 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>Writer:</th><td><select name="writer">
+<option value="" selected="selected">---------</option>
+<option value="1">Mike Royko</option>
+<option value="2">Bob Woodward</option>
+</select></td></tr>
<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
<option value="1">Entertainment</option>
<option value="2">It's a test</option>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---