簡體   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