Why can't I set my Django Model's USPhoneNumberField to null=True, blank=True?

495 Views Asked by At

My Django Model:

from localflavor.us.forms import USPhoneNumberField

class Profile(models.Model):
    cellPhone = USPhoneNumberField(null=True, blank=True,)

This gives me the following error when I do a manage.py syncdb:

cellPhone = USPhoneNumberField(null=True, blank=True,)
TypeError: __init__() got an unexpected keyword argument 'null'

How can I make this field optional if it won't let me declare it as null=True like I do for so many other fields?

1

There are 1 best solutions below

0
On BEST ANSWER

USPhoneNumberField is a form type, not a model type

The usage (typically) is:

class Profile(models.Model):
    cellPhone = models.CharField(max_length=20, null=True, blank=True,)

and forms.py

class ProfileForm(forms.Form): #or forms.ModelForm
    cellPhone = forms.USPhoneNumberField(...)

Here is the documentation

class localflavor.us.models.PhoneNumberField(*args, **kwargs) A CharField that checks that the value is a valid U.S.A.-style phone number (in the format XXX-XXX-XXXX).