diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index ce33968974..eb388fcb6d 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -361,7 +361,7 @@ def create_many_related_manager(superclass): 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 @@ class ForeignKey(RelatedField, Field): 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: diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index da10e215a6..566dad42ea 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -20,9 +20,16 @@ class Category(models.Model): 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 @@ Traceback (most recent call last): ... 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 Headline: Pub date: +Writer: Categories: