diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 2027b2c85af..024fa95b8e7 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -336,7 +336,7 @@ class Field(object): def formfield(self, **kwargs): "Returns a django.newforms.Field instance for this database Field." - defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)} + defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.CharField(**defaults) @@ -398,7 +398,7 @@ class BooleanField(Field): return [oldforms.CheckboxField] def formfield(self, **kwargs): - defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)} + defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.BooleanField(**defaults) @@ -417,7 +417,7 @@ class CharField(Field): return str(value) def formfield(self, **kwargs): - defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name)} + defaults = {'max_length': self.maxlength, 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.CharField(**defaults) @@ -493,7 +493,7 @@ class DateField(Field): return {self.attname: (val is not None and val.strftime("%Y-%m-%d") or '')} def formfield(self, **kwargs): - defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)} + defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.DateField(**defaults) @@ -556,7 +556,7 @@ class DateTimeField(DateField): time_field: (val is not None and val.strftime("%H:%M:%S") or '')} def formfield(self, **kwargs): - defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)} + defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.DateTimeField(**defaults) @@ -575,7 +575,7 @@ class EmailField(CharField): validators.isValidEmail(field_data, all_data) def formfield(self, **kwargs): - defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)} + defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.EmailField(**defaults) @@ -712,7 +712,7 @@ class IntegerField(Field): return [oldforms.IntegerField] def formfield(self, **kwargs): - defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)} + defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.IntegerField(**defaults) @@ -771,7 +771,7 @@ class TextField(Field): return [oldforms.LargeTextField] def formfield(self, **kwargs): - defaults = {'required': not self.blank, 'widget': forms.Textarea, 'label': capfirst(self.verbose_name)} + defaults = {'required': not self.blank, 'widget': forms.Textarea, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.CharField(**defaults) @@ -816,7 +816,7 @@ class TimeField(Field): return {self.attname: (val is not None and val.strftime("%H:%M:%S") or '')} def formfield(self, **kwargs): - defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name)} + defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.TimeField(**defaults) @@ -835,7 +835,7 @@ class URLField(CharField): return "CharField" def formfield(self, **kwargs): - defaults = {'required': not self.blank, 'verify_exists': self.verify_exists, 'label': capfirst(self.verbose_name)} + defaults = {'required': not self.blank, 'verify_exists': self.verify_exists, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.URLField(**defaults) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 7c373792b2d..384b05d2184 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -549,7 +549,7 @@ class ForeignKey(RelatedField, Field): setattr(cls, related.get_accessor_name(), ForeignRelatedObjectsDescriptor(related)) def formfield(self, **kwargs): - defaults = {'choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name)} + defaults = {'choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.ChoiceField(**defaults) @@ -615,7 +615,7 @@ class OneToOneField(RelatedField, IntegerField): cls._meta.one_to_one_field = self def formfield(self, **kwargs): - defaults = {'choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name)} + defaults = {'choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.ChoiceField(**kwargs) @@ -738,7 +738,7 @@ class ManyToManyField(RelatedField, Field): # MultipleChoiceField takes a list of IDs. if kwargs.get('initial') is not None: kwargs['initial'] = [i._get_pk_val() for i in kwargs['initial']] - defaults = {'choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name)} + defaults = {'choices': self.get_choices_default(), 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} defaults.update(kwargs) return forms.MultipleChoiceField(**defaults) diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 81a7ec2abf7..657d506b337 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -32,7 +32,7 @@ class Category(models.Model): return self.name class Writer(models.Model): - name = models.CharField(maxlength=50) + name = models.CharField(maxlength=50, help_text='Use both first and last names.') def __str__(self): return self.name @@ -150,7 +150,7 @@ represented by a ChoiceField. - +
Hold down "Control", or "Command" on a Mac, to select more than one. You can pass a custom Form class to form_for_model. Make sure it's a subclass of BaseForm, not Form. @@ -169,7 +169,7 @@ current values are inserted as 'initial' data in each Field. >>> RoykoForm = form_for_instance(w) >>> f = RoykoForm(auto_id=False) >>> print f -Name: +Name:
Use both first and last names. >>> art = Article(headline='Test article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.') >>> art.save() @@ -190,7 +190,7 @@ current values are inserted as 'initial' data in each Field. - + Hold down "Control", or "Command" on a Mac, to select more than one. >>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', 'writer': u'1', 'article': 'Hello.'}) >>> f.is_valid() True @@ -222,7 +222,7 @@ Add some categories and test the many-to-many form output. - + Hold down "Control", or "Command" on a Mac, to select more than one. >>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', ... 'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']})