gpt4 book ai didi

python - 尝试同步数据库时出现类错误(Python Django)

转载 作者:行者123 更新时间:2023-12-01 04:48:26 24 4
gpt4 key购买 nike

尝试通过命令行同步数据库时出现错误。

我的目标是一个具有特定条件的基本音乐搜索/删除/更新/添加应用程序,我正在使用 Python 和 Django。

我正在尝试编写的类(class):

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'

最佳答案

我发现您的模型存在两个问题。

您没有创建IntegerField的实例;你需要调用它:

id = models.IntegerField()
# ^^

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

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

删除这些逗号。

您实际上不必指定自己的 id 字段;默认情况下,模型会自动指定一个 id 字段。请参阅Automatic primary fields在 Django 模型文档中:

By default, Django gives each model the following field:

id = models.AutoField(primary_key=True)

This is an auto-incrementing primary key.

由于您指定了自己的 id 字段,而使用 primary_key=True,因此您的模型可能会遇到以下问题:也是如此。

关于python - 尝试同步数据库时出现类错误(Python Django),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28917252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com