繁体   English   中英

在 Django 中使用文本字段和字符字段 model 时出错

[英]Error using Textfield and charfield model in Django

我一直在尝试在 class 中使用 Textfield,但建议没有出现。 相反,我在 Cmd 控制台中收到错误消息。 甚至在使用 CharField 时也是如此。

下面是我的代码:

from django.db import models 
from django.db.models import CharField
from django.db.models import Textfield

from datetime import datetime

class Posts(models.Model):
  title = models.CharField(max_length=200)
  body = models.TextField()
  created_at = models.DateTimeField(default=datetime.now, blank=True)

这是我在 CMD 中遇到的错误:

Exception in thread django-main-thread:
Traceback (most recent call last):
File "d:\python\python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "d:\python\python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\core\management\commands\runserver.py", line 
109, in inner_run
autoreload.raise_last_exception()
File "C:\Users\USER\Envs\py1\lib\site-packages\django\utils\autoreload.py", line 76, in 
raise_last_exception
raise _exception[1]
File "C:\Users\USER\Envs\py1\lib\site-packages\django\core\management\__init__.py", line 357, in 
execute
autoreload.check_errors(django.setup)()
File "C:\Users\USER\Envs\py1\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\USER\Envs\py1\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\USER\Envs\py1\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "d:\python\python38\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 "E:\Python\djangoproject\posts\models.py", line 3, in <module>
from django.db.models import Textfield
ImportError: cannot import name 'Textfield' from 'django.db.models' (C:\Users\USER\Envs\py1\lib\site- 
packages\django\db\models\__init__.py)

其实你不需要单独导入字段,你可以使用之前导入的 django.db.models 。

并且产生错误是因为您应该使用带有大写FTextField

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM