0

我在尝试初始化一些数据时遇到了麻烦。

当我运行 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)
4

1 回答 1

0

您没有展示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 原则之一)——您的子类实际上不允许用作父类。

于 2016-11-29T02:21:29.590 回答