如何在forms.py中使用django-phonenumber_field?

时间:2017-10-18 12:50:25

标签: django

我试图将用户的电话号码作为个人资料更新的一部分。我最近从stackoverflow发现了django-phonenumber_field库,我可以轻松地输入电话号码。所以我在model.py这样使用它

phone = PhoneNumberField(blank=True)

正如django-phonenumber_field github repository here

中提到的那样

  

与CharField一样,不鼓励使用null = True。

所以我也没有使用它。然后我尝试在phone field中使用forms.py。最初我在了解phone field

之前就已经使用了这个

phone = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'form-control'}), required=True)

当我将其更改为phone = PhoneNumberField()

我正在迁移,它提出了field error。 。我找不到任何forms.PhoneNumberField()。因此,当我将该字段保持为Integerfield并运行服务器时,我试图从用户那里得到输入我得到了这个错误:

NOT NULL constraint failed: auth_profile.phone

然后我将phone = PhoneNumberField(blank=True)更改为phone = PhoneNumberField(null=True, blank=True).,这是不鼓励的。之后,虽然我没有收到任何错误,但phone-field中的用户类型总是在数据库中设置null并不重要。可能是因为我使用了Integerfield我保留在上面提到的forms.py中。或者我不知道原因。

如何在PhoneNumberField()或任何机制中添加form,以便在数据库中保存用户给定的值?

修改 正如@Sam Bobel告诉我发布详细信息models.py,这里是:

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

现在如果我运行迁移,我会

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: 

**forms.py **:

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']

2 个答案:

答案 0 :(得分:4)

以下是我在项目中使用phonenumber字段的方法,到现在为止它没有任何问题。我也没有遇到任何迁移问题。

内幕模特:

from phonenumber_field.modelfields import PhoneNumberField

phonenumber = PhoneNumberField(blank=True)

内部形式:

from phonenumber_field.formfields import PhoneNumberField

class ExampleForm(forms.ModelForm):

    phonenumber = PhoneNumberField(widget=forms.TextInput(attrs={'placeholder': _('Phone')}), 

                       label=_("Phone number"), required=False)

答案 1 :(得分:0)

看起来你正试图从错误的地方导入PhoneNumberField。如果我理解正确,forms.PhoneNumberField就不存在了。

首先,请确保phonenumber_field在您已安装的应用中(图书馆的自述文件显示了如何执行此操作。

其次,请确保您正确导入。 from phonenumber_field.modelfields import PhoneNumberField应位于models.py

的顶部

第三,在你的模型中,说`field_name = PhoneNumberField(blank = True)

然后你应该能够进行迁移。

如果这偏离基础,你会更全面地发布你的模型代码吗?