首页 新闻 会员 周边 捐助

Django修改数据库发生错误

0
悬赏园豆:20 [已解决问题] 解决于 2018-04-12 17:22

新人求问,正在学习python做博客,

from django.db import models

Class Topic(models.Model):
      """用户学习的主题"""
      text = models.CharField(max_length=200)
      date_added = models.DateTimeField(auto_now_add=True)

      def __srt__(self):
          """返回模型的字符串表示"""
          return self.text
在models.py中运行上面的代码提示出错:
File "E:/Python/learning_log/env1/Scripts/learning_logs/models.py", line 3
Class Topic(models.Model):
^
SyntaxError: invalid syntax

请问是什么原因?

另外在命令行执行python manage.py makemigrations也报错:

复制代码
E:\Python\learning_log\env1\Scripts>activate

(env1) E:\Python\learning_log\env1\Scripts>python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "E:\Python\learning_log\env1\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "E:\Python\learning_log\env1\lib\site-packages\django\core\management\__init__.py", line 347, in execute
    django.setup()
  File "E:\Python\learning_log\env1\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "E:\Python\learning_log\env1\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "E:\Python\learning_log\env1\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "E:\Python\learning_log\env1\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 674, in exec_module
  File "<frozen importlib._bootstrap_external>", line 781, in get_code
  File "<frozen importlib._bootstrap_external>", line 741, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "E:\Python\learning_log\env1\Scripts\learning_logs\models.py", line 3
    Class Topic(models.Model):
              ^
SyntaxError: invalid syntax
复制代码
Twinko的主页 Twinko | 初学一级 | 园豆:184
提问于:2018-04-12 14:43
< >
分享
最佳答案
1

很明显,text = models.CharField(max_length=200)定义的顶格了,注意缩进,还有最后的return self.text,也在def __srt__(self)里

1 class Topic(models.Model):
2 """用户学习的主题"""
3     text = models.CharField(max_length=200)
4     date_added = models.DateTimeField(auto_now_add=True)
5 
6     def __srt__(self):
7         """返回模型的字符串表示"""
8         return self.text
收获园豆:20
北方姆Q | 小虾三级 |园豆:1120 | 2018-04-12 15:08

格式没有问题 只不过粘贴过来时候格式乱了。。

Twinko | 园豆:184 (初学一级) | 2018-04-12 15:32

@Twinko: class关键字首字母小写

北方姆Q | 园豆:1120 (小虾三级) | 2018-04-12 15:57

@北方姆Q: 改成小写之后还是报错:

E:\Python\learning_log\env1\Scripts\python.exe E:/Python/learning_log/env1/Scripts/learning_logs/models.py
Traceback (most recent call last):
  File "E:/Python/learning_log/env1/Scripts/learning_logs/models.py", line 3, in <module>
    class Topic(models.Model):
  File "E:\Python\learning_log\env1\lib\site-packages\django\db\models\base.py", line 100, in __new__
    app_config = apps.get_containing_app_config(module)
  File "E:\Python\learning_log\env1\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config
    self.check_apps_ready()
  File "E:\Python\learning_log\env1\lib\site-packages\django\apps\registry.py", line 127, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Twinko | 园豆:184 (初学一级) | 2018-04-12 16:14

@Twinko: 嗯,这个你要去你的setting里的INSTALLED_APPS的大列表内添加你的这个app的名称

北方姆Q | 园豆:1120 (小虾三级) | 2018-04-12 16:46

@北方姆Q: 已经添加了'learning_logs', 还是报错。

Twinko | 园豆:184 (初学一级) | 2018-04-12 16:52

@Twinko: python manage.py makemigrations

python manage.py migrate

这两个现在有报错嘛,还是已经通过了?

北方姆Q | 园豆:1120 (小虾三级) | 2018-04-12 16:57

@北方姆Q: 已经通过啦 

Twinko | 园豆:184 (初学一级) | 2018-04-12 17:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册