You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django模型中PhoneNumberField无法识别问题求助

解决django-phonenumber-field模块找不到的问题

问题描述

开发Django REST Framework API时,为存储用户手机号选择了django-phonenumber-field包,已安装django-phonenumber-field==5.0.0和phonenumberslite==8.12.11,但执行makemigrations时出现ModuleNotFoundError: No module named 'phonenumber_field'。

报错堆栈

Traceback (most recent call last):
  File "C:\Users\moham\OneDrive\Projects\UpMenu\manage.py", line 24, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\moham\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\moham\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\moham\AppData\Local\Programs\Python\Python310\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\moham\AppData\Local\Programs\Python\Python310\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\moham\AppData\Local\Programs\Python\Python310\lib\site-packages\django\apps\config.py", line 90, in create
    module = import_module(entry)
  File "C:\Users\moham\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'phonenumber_field'

用到的models.py代码

import uuid
from django.db import models
from phonenumber_field.modelfields import PhoneNumberField

# Create your models here.


class Users(models.Model):
    owner = models.ForeignKey(
        "auth.User", related_name="snippets", on_delete=models.CASCADE, null=True
    )
    Id = models.UUIDField(
        default=uuid.uuid4, unique=True, primary_key=True, editable=False
    )
    Email = models.EmailField(unique=True)
    Password = models.CharField(max_length=50)
    FirstName = models.CharField(max_length=50)
    LastName = models.CharField(max_length=50)
    Address = models.CharField(max_length=250)
    CellPhone = PhoneNumberField()
    EmailActiveCode = models.BooleanField(default=False)
    IsActived = models.BooleanField(default=False)
    IsDelete = models.BooleanField(default=False)
    CreatedDate = models.DateTimeField(auto_now_add=True)
    LastUpdateDate = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["CreatedDate"]

解决方案

  • 注册应用到INSTALLED_APPS:打开项目的settings.py,在INSTALLED_APPS列表中添加'phonenumber_field',这是Django第三方应用的必填步骤:

    INSTALLED_APPS = [
        # 其他默认应用
        'phonenumber_field',
        # 你的自定义应用
    ]
    
  • 确认虚拟环境安装正确:如果使用了虚拟环境,确保执行安装命令时已经激活了项目对应的虚拟环境。若之前安装到了全局Python环境,重新激活虚拟环境后执行:

    pip install django-phonenumber-field==5.0.0 phonenumberslite==8.12.11
    
  • 验证包安装状态:运行pip show django-phonenumber-field,查看输出的Location路径是否与项目虚拟环境的site-packages目录一致,确认包确实安装在当前项目使用的Python环境中。

  • 检查IDE解释器配置:如果使用PyCharm等IDE,进入项目设置,确认Python解释器选择的是安装了该包的虚拟环境解释器,避免IDE使用全局解释器导致找不到模块。

内容的提问来源于stack exchange,提问作者Mhmdnzr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 16:42:28