python - 如何为没有字段的模型制作 Django 固定装置?

标签 python django django-models yaml django-fixtures

如果我有一个 Django 模型,定义了一些字段:

# model.py

from django.db import models


class Model(models.Model):
    text = models.CharField(max_length=10)

我可以使用夹具对其进行初始化:
# sample.yaml

- model: app.Model
  pk: 1
  fields:
    text: "some text"

使用命令:manage.py loaddata sample.yaml一切正常。

我的问题是我不能对没有字段的模型做同样的事情 :
# model.py

from django.db import models


class Model(models.Model):
    pass
# sample.yaml

- model: app.Model
  pk: 1
  fields:

然后同样manage.py loaddata sample.yaml命令给出错误:

Traceback (most recent call last):
 File "/usr/local/lib/python3.8/site-packages/django/core/serializers/pyyaml.py", line 73, in Deserializer
   yield from PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options)
 File "/usr/local/lib/python3.8/site-packages/django/core/serializers/python.py", line 112, in Deserializer
   for (field_name, field_value) in d["fields"].items():
AttributeError: 'NoneType' object has no attribute 'items'

The above exception was the direct cause of the following exception:

   Traceback (most recent call last):
     File "manage.py", line 23, in <module>
       execute_from_command_line(sys.argv)
     File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
       utility.execute()
     File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
       self.fetch_command(subcommand).run_from_argv(self.argv)
     File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv
       self.execute(*args, **cmd_options)
     File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 364, in execute
       output = self.handle(*args, **options)
     File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
       self.loaddata(fixture_labels)
     File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 114, in loaddata
       self.load_label(fixture_label)
     File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 172, in load_label
       for obj in objects:
     File "/usr/local/lib/python3.8/site-packages/django/core/serializers/pyyaml.py", line 77, in Deserializer
       raise DeserializationError() from exc
   django.core.serializers.base.DeserializationError: Problem installing fixture '/app/src/app/fixtures/sample.yaml':


我也试过不指定 fields根本:
# sample.yaml

- model: app.Model
  pk: 1

我得到了一个类似但不同的错误:

Traceback (most recent call last):
 File "/usr/local/lib/python3.8/site-packages/django/core/serializers/pyyaml.py", line 73, in Deserializer
   yield from PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options)
 File "/usr/local/lib/python3.8/site-packages/django/core/serializers/python.py", line 112, in Deserializer
   for (field_name, field_value) in d["fields"].items():
KeyError: 'fields'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
 File "manage.py", line 23, in <module>
   execute_from_command_line(sys.argv)
 File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
   utility.execute()
 File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 375, in execute
   self.fetch_command(subcommand).run_from_argv(self.argv)
 File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 323, in run_from_argv
   self.execute(*args, **cmd_options)
 File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 364, in execute
   output = self.handle(*args, **options)
 File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
   self.loaddata(fixture_labels)
 File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 114, in loaddata
   self.load_label(fixture_label)
 File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/loaddata.py", line 172, in load_label
   for obj in objects:
 File "/usr/local/lib/python3.8/site-packages/django/core/serializers/pyyaml.py", line 77, in Deserializer
   raise DeserializationError() from exc
django.core.serializers.base.DeserializationError: Problem installing fixture '/app/src/app/fixtures/sample.yaml':

最佳答案

YAML 的工作方式与 JSON 几乎相同,因此我们可以简单地指定 fields成为 empty dictionary :

# sample.yaml

- model: app.Model
  pk: 1
  fields: {}

关于python - 如何为没有字段的模型制作 Django 固定装置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59076832/

相关文章:

python - 何时将 http 方法映射到查看方法 django rest framework

python - 在 Django 模型的初始化方法中访问父字段

python - 使用任意方法或属性作为 Django ModelAdmin 对象的字段?

python - 将 MySQLdb Python 模块与 mysql_old_password 一起使用

Python - 查找列表中子列表的长度,忽略空条目

django - 在 Django QuerySet 中,如何检查 ManyToMany 字段中的特定对象?

Django 删除多个 m2m 关系而不循环

python - 'str' 对象不支持项目分配 - django 错误

python - 无法从 Keras 导入 Tokenizer

javascript - 在Django中,如何使选择下拉选项自动提交表单?