title: How to use django-phonenumber_field in forms.py?
body: <p>I was trying to take phone number from user as part of his profile updating. I recently found out from <code>stackoverflow</code> that there's a library <code>django-phonenumber_field</code> with which I can easily take input the phone number. So I used it in the <code>model.py</code> like this </p>
  
  <p><code>phone = PhoneNumberField(blank=True)</code>. </p>
  
  <p>As it is mentioned in the <code>django-phonenumber_field github repository</code> <a href="https://github.com/stefanfoulis/django-phonenumber-field" rel="noreferrer">here</a> </p>
  
  <p>that </p>
  
  <blockquote>
    <p>As with CharField's, it is discouraged to use null=True.</p>
  </blockquote>
  
  <p>So I didn't use that too. Then I tried to use that <code>phone field</code> in <code>forms.py</code>. Initially I was using this before came to know about <code>phone field</code>:</p>
  
  <p><code>phone = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'form-control'}), required=True)</code></p>
  
  <p>When I am changing it to <code>phone = PhoneNumberField()</code></p>
  
  <p>and I'm migrating, it raised <code>field error</code>. . I coudln't find any <code>forms.PhoneNumberField()</code>. So when I'm keeping the field to be the <code>Integerfield</code> and running the server, I tried to get the input from user I get this error:</p>
  
  <p><code>NOT NULL constraint failed: auth_profile.phone</code></p>
  
  <p>Then I changed <code>phone = PhoneNumberField(blank=True)</code> to <code>phone = PhoneNumberField(null=True, blank=True).</code>  which is discouraged. After that, though I didn't get any error but it doesn't matter what user type in the <code>phone-field</code> it always sets <code>null</code> in the database. Probably because I used <code>Integerfield</code> I kept in the <code>forms.py</code> mentioned above. Or I don't know the reason. </p>
  
  <p>How can I add the <code>PhoneNumberField()</code> in the <code>form</code> or any mechanism so that in the database, user given value will be saved?</p>
  
  <p><strong>Edit</strong>
  As @Sam Bobel told me to post the <code>models.py</code> in details, here it is:</p>
  
  <pre><code>from __future__ import unicode_literals
  from django.contrib.auth.models import User
  from django.db import models
  from django.db.models.signals import post_save
  from django.utils.encoding import python_2_unicode_compatible
  from django.dispatch import receiver
  from phonenumber_field.modelfields import PhoneNumberField
  
  @python_2_unicode_compatible
  class Profile(models.Model):
      user = models.OneToOneField(User)
      # location = models.CharField(max_length=50, null=True, blank=True)
      # url = models.CharField(max_length=50, null=True, blank=True)
      # job_title = models.CharField(max_length=50, null=True, blank=True)
      user_sex = (('MALE', 'Male'), ('FEMALE', 'Female'))
      sex = models.CharField(max_length=6, default='Male', choices=user_sex)
      address = models.CharField(max_length=250, null=True, blank=True)
      city = models.CharField(max_length=250, null=True, blank=True)
      state = models.CharField(max_length=250, null=True, blank=True)
      country = models.CharField(max_length=250, null=True, blank=True)
      phone = PhoneNumberField(null=True)
      zip = models.IntegerField(null=True, blank=True)
      about = models.CharField(max_length=250, null=True, blank=True)
      email_confirmed = models.BooleanField(default=False)
      account_type = models.IntegerField(default=-1)
  
      class Meta:
          db_table = 'auth_profile'
  
      def __str__(self):
          return self.user.username
  </code></pre>
  
  <p>Now if I run migrations, I get</p>
  
  <pre><code>You are trying to change the nullable field 'phone' on profile to non-nullable without a default; we can't do that (the database needs something to populate existing rows).
  Please select a fix:
   1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
   2) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration)
   3) Quit, and let me add a default in models.py
  Select an option: 
  </code></pre>
  
  <p><code>**forms.py</code>**:</p>
  
  <pre><code>class ContactForm(forms.ModelForm):
  
      address = forms.CharField(
          widget=forms.Textarea(attrs={'class': 'form-control', 'rows': 4}),
          max_length=300,
          required=True)
      city = forms.CharField(
          widget=forms.TextInput(attrs={'class': 'form-control'}),
          max_length=30,
          required=True)
      state = forms.CharField(
          widget=forms.TextInput(attrs={'class': 'form-control'}),
          max_length=30,
          required=True)
      country = forms.CharField(
          widget=forms.TextInput(attrs={'class': 'form-control'}),
          max_length=30,
          required=True)
      phone = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'form-control'}),
          required=True)
      zip = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'form-control'}),
          required=True)
  
      class Meta:
          model = User
          fields = ['address', 'city', 'state', 'country', 'phone', 'zip']
  </code></pre>