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 原則の 1 つ) を破っています — サブクラスは実際には親クラスとしての使用を許可していません。

于 2016-11-29T02:21:29.590 に答える