diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 0f8a1cdee2..953026dcfe 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -748,6 +748,12 @@ class PhoneNumberField(IntegerField): def validate(self, field_data, all_data): validators.isValidPhone(field_data, all_data) + def formfield(self, **kwargs): + from django.contrib.localflavor.usa.forms import USPhoneNumberField + defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text} + defaults.update(kwargs) + return USPhoneNumberField(**defaults) + class PositiveIntegerField(IntegerField): def get_manipulator_field_objs(self): return [oldforms.PositiveIntegerField] diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py index 38829ad662..e64174a23f 100644 --- a/tests/modeltests/model_forms/models.py +++ b/tests/modeltests/model_forms/models.py @@ -54,6 +54,13 @@ class Article(models.Model): def __str__(self): return self.headline +class PhoneNumber(models.Model): + phone = models.PhoneNumberField() + description = models.CharField(maxlength=20) + + def __str__(self): + return self.phone + __test__ = {'API_TESTS': """ >>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField >>> import datetime @@ -445,4 +452,13 @@ ValidationError: [u'Select a valid choice. 10 is not one of the available choice Traceback (most recent call last): ... ValidationError: [u'Select a valid choice. 10 is not one of the available choices.'] + +# PhoneNumberField ############################################################ + +>>> PhoneNumberForm = form_for_model(PhoneNumber) +>>> f = PhoneNumberForm({'phone': '(312) 555-1212', 'description': 'Assistance'}) +>>> f.is_valid() +True +>>> f.clean_data +{'phone': u'312-555-1212', 'description': u'Assistance'} """}