如何运行迁移并添加电话号码

Posted

技术标签:

【中文标题】如何运行迁移并添加电话号码【英文标题】:How do I run migrations and add a phonenumberfied as well 【发布时间】:2021-11-27 09:55:25 【问题描述】:

我正在创建一个带有电话号码字段的 django 模型表单。我已经 pip 安装了 phonenumber 字段模块并将其添加到我已安装的应用程序中,但现在当我尝试运行迁移以将此字段的表单添加到我的数据库中时。我得到这个错误。我做错了什么。

File "C:\Users\houst\CMS\TheMachine\manage.py", line 22, in <module>
main()
File "C:\Users\houst\CMS\TheMachine\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\houst\Envs\Mach\lib\site-packages\django\core\management\__init__.py", line 
419, in execute_from_command_line  
utility.execute()
File "C:\Users\houst\Envs\Mach\lib\site-packages\django\core\management\__init__.py", line 
413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)     
File "C:\Users\houst\Envs\Mach\lib\site-packages\django\core\management\base.py", line 354, in 
run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\houst\Envs\Mach\lib\site-packages\django\core\management\base.py", line 398, in 
execute
output = self.handle(*args, **options)
File "C:\Users\houst\Envs\Mach\lib\site-packages\django\core\management\base.py", line 89, in 
wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\houst\Envs\Mach\lib\site- 
packages\django\core\management\commands\makemigrations.py", line 149, in handle      
loader.project_state(),
File "C:\Users\houst\Envs\Mach\lib\site-packages\django\db\migrations\loader.py", line 335, in 
project_state
return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
File "C:\Users\houst\Envs\Mach\lib\site-packages\django\db\migrations\graph.py", line 315, in 
make_state
project_state = self.nodes[node].mutate_state(project_state, preserve=False)
File "C:\Users\houst\Envs\Mach\lib\site-packages\django\db\migrations\migration.py", line 89, 
in mutate_state
operation.state_forwards(self.app_label, new_state)
File "C:\Users\houst\Envs\Mach\lib\site-packages\django\db\migrations\operations\models.py", 
line 80, in state_forwards       
state.add_model(ModelState(
File "C:\Users\houst\Envs\Mach\lib\site-packages\django\db\migrations\state.py", line 373, in 
__init__
 if field.is_relation and hasattr(field.related_model, '_meta'):
AttributeError: 'PhoneNumberField' object has no attribute 'is_relation'

这是我的model.py

from django.db import models
from phonenumber_field.formfields import PhoneNumberField

class Profile(models.Model):
 Name = models.CharField(max_length=100)
 Last_Name = models.CharField(max_length=100)
 phone_number = PhoneNumberField()
 Address_Line_1 = models.CharField(max_length=300)
 Address_Line_2 = models.CharField(max_length=300)
 Postcode = models.IntegerField()
 State = models.CharField(max_length=100)
 Area = models.IntegerField()
 Email = models.EmailField()
 Country = models.CharField(max_length=300)
 State_Region = models.CharField(max_length=100)

 def __str__(self):
    return self.Name + ' ' + self.State_Region

forms.py

from .models import Appear, Performance, Profile2, Publicity, Bookings, Profile
from phonenumber_field.formfields import PhoneNumberField

class Profiles(forms.ModelForm):

   phone_number = PhoneNumberField()

   class Meta:
      model = Profile
      fields = "__all__"

【问题讨论】:

【参考方案1】:

您现在可能已经解决了(或放弃了),但以防万一:问题是您将 表单字段 放入模型中,但您需要一个 模型字段。这很令人困惑,它一直在发生。如果您将 models.py 中的导入更改为 from phonenumber_field.modelfields import PhoneNumberField,它应该可以工作。

【讨论】:

以上是关于如何运行迁移并添加电话号码的主要内容,如果未能解决你的问题,请参考以下文章

如何让 android 应用程序等到电话完成?

如何将字符串号码格式化为美国电话号码格式

如何添加到现有号码? C++

PHP - 如何添加小数,.我的号码不加 00?

如何将电话号码字段添加到 django UserCreationForm?

如何通过 Intent 向现有联系人添加电话号码?