首页 > 解决方案 > 在已添加到现有模型的产品模型字段上运行 makemigrate 时出错

问题描述

诚然,我是 django 的新手,尝试运行python manage.py makemigrations时出现以下错误

我正在尝试在我的类 Product(models.Model) 中添加一个新字段:featured = models.BooleanField()

代码

from django.db import models

# Create your models here.
class Product(models.Model):
    title       = models.CharField(max_length=120) # max_length = required
    description     = models.TextField(blank=True, null=True)
    price       = models.DecimalField(decimal_places=2, max_digits=10000)
    summary     = models.TextField()
    featured        = models.BooleenField()  #only new added line

错误:AttributeError:模块“django.db.models”没有属性“BooleenField”

command:
python manage.py makemigrations <enter>

entire_traceback:
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\.....\trydjango\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\.....\trydjango\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\.....\trydjango\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\.....\trydjango\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "C:\.....\trydjango\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\.....\trydjango\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\.....\src\products\models.py", line 4, in <module>
    class Product(models.Model):
  File "C:\.....\src\products\models.py", line 9, in Product
    featured    = models.BooleenField()
AttributeError: module 'django.db.models' has no attribute 'BooleenField'

标签: pythondjangopython-3.xdjango-models

解决方案


产品模型的代码中存在错误。我已经纠正了。

from django.db import models

class Product(models.Model):
    title       = models.CharField(max_length=120) # max_length = required
    description = models.TextField(blank=True, null=True)
    price       = models.DecimalField(decimal_places=2, max_digits=10000)
    summary     = models.TextField()
    featured    = models.BooleanField()  #only new added line

布尔字段的默认值始终为 false。


推荐阅读