Django loaddata 错误 __init__() 正好需要 3 个参数(给定 1 个)
Posted
技术标签:
【中文标题】Django loaddata 错误 __init__() 正好需要 3 个参数(给定 1 个)【英文标题】:Django loaddata error __init__() takes exactly 3 arguments (1 given) 【发布时间】:2017-04-12 21:34:34 【问题描述】:我在尝试初始化一些数据时遇到问题。
当我运行 manage.py loaddata --verbosity=1 initial.json
以此作为我的 initial.json 文件:
[
"model": "listen.Playlist",
"pk": 1,
"fields":
"message": "Hello There!",
"url": "pl8675309",
"background": "citylights.png"
]
我得到的输出是:
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/django/core/management/commands/loaddata.py", line 64, in handle
self.loaddata(fixture_labels)
File "/Library/Python/2.7/site-packages/django/core/management/commands/loaddata.py", line 104, in loaddata
self.load_label(fixture_label)
File "/Library/Python/2.7/site-packages/django/core/management/commands/loaddata.py", line 161, in load_label
for obj in objects:
File "/Library/Python/2.7/site-packages/django/core/serializers/json.py", line 86, in Deserializer
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
File "/Library/Python/2.7/site-packages/django/core/serializers/json.py", line 80, in Deserializer
for obj in PythonDeserializer(objects, **options):
File "/Library/Python/2.7/site-packages/django/core/serializers/python.py", line 183, in Deserializer
obj = base.build_instance(Model, data, db)
File "/Library/Python/2.7/site-packages/django/core/serializers/base.py", line 218, in build_instance
obj = Model(**data)
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 556, in __init__
super(Model, self).__init__()
django.core.serializers.base.DeserializationError: Problem installing fixture 'initial.json': __init__() takes exactly 3 arguments (1 given)
【问题讨论】:
你的Playlist
模型是什么样的?
谢谢。只是看着我的模型删除了不需要的东西并修复了它。我很感激!
【参考方案1】:
您没有显示Minimal Complete Verifiable Example,因此我们无法重现您的代码。
我可以猜到您编写的 Model 子类有一个自定义的__init__
,它意外地需要更多参数:
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 556, in __init__
super(Model, self).__init__()
django.core.serializers.base.DeserializationError: Problem installing fixture 'initial.json': __init__() takes exactly 3 arguments (1 given)
如果是这样,您就违反了 Liskov 替换原则(SOLID principles 之一)——您的子类实际上不允许用作父类。
【讨论】:
以上是关于Django loaddata 错误 __init__() 正好需要 3 个参数(给定 1 个)的主要内容,如果未能解决你的问题,请参考以下文章
Django--bug--__init__() got an unexpected keyword argument 'qnique'
简单的留言簿 django:__init__() 接受 1 个位置参数,但给出了 2 个
Django __init__() 得到了一个意外的关键字参数“用户”