尝试syncDB时类中的错误(Python Django)

时间:2015-03-07 16:41:57

标签: python django class

尝试通过命令行syncdb时遇到错误。

我的目标是使用某些标准进行基本的音乐搜索/删除/更新/添加应用程序,我使用的是Python和Django。

我正在写的课程:

from django.db import models

# music search
class musicsearch(models.Model):
    id = models.IntegerField
    title = models.CharField(max_length=40)
    artist = models.CharField(max_length=40)
    genre = models.CharField(max_length=20)

错误追溯:

C:\Users\jodie\Desktop\NtokloMH>python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 354, in execute
    django.setup()
  File "C:\Python34\lib\site-packages\django\__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Python34\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:\Python34\lib\site-packages\django\apps\config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "C:\Users\jodie\Desktop\NtokloMH\musicsearch\models.py", line 4, in <module>
    class musicsearch(models.Model):
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 168, in __new__
    new_class.add_to_class(obj_name, obj)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 297, in add_to_class
    value.contribute_to_class(cls, name)
TypeError: contribute_to_class() missing 1 required positional argument: 'name'

2 个答案:

答案 0 :(得分:2)

我可以看到您的模型存在两个问题。

您没有创建IntegerField的实例;你需要打电话给它:

id = models.IntegerField()
#                       ^^

您正在为其他字段创建元组,因为您使用逗号结束每个字段:

title = models.CharField(max_length=40),
#                                      ^

删除这些逗号。

您不必指定自己的id字段;默认情况下,模型会自动给出id字段。请参阅Django模型文档中的Automatic primary fields

  

默认情况下,Django为每个模型提供以下字段:

id = models.AutoField(primary_key=True)
     

这是一个自动递增的主键。

由于您指定了 使用id的{​​{1}}字段,因此您的模型可能会遇到问题。

答案 1 :(得分:0)

你的id整数字段缺少两个()。

id = models.IntegerField()