简体   繁体   English

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

[英]How to make a Django fixture for a model with no fields?

If I have a Django model, with some field(s) defined:如果我有一个 Django 模型,定义了一些字段:

# model.py

from django.db import models


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

I can initialize it by using a fixture:我可以使用夹具对其进行初始化:

# sample.yaml

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

with the command: manage.py loaddata sample.yaml and everything works fine.使用命令: manage.py loaddata sample.yaml一切正常。

My problem is that I cannot do the same for a model with no fields :我的问题是我不能对没有字段的模型做同样的事情

# model.py

from django.db import models


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

- model: app.Model
  pk: 1
  fields:

Then the same manage.py loaddata sample.yaml command gives an error:然后同样的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':

I also tried without specifying fields at all:我也试过根本不指定fields

# sample.yaml

- model: app.Model
  pk: 1

and I get a similar but different error:我得到了一个类似但不同的错误:

 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 works pretty much the same way as JSON does, so we can simply specify fields to be an empty dictionary : YAML 的工作方式与 JSON 几乎相同,因此我们可以简单地将fields指定为空字典

# sample.yaml

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

It works using a JSON file.它使用 JSON 文件工作。

[
  {
    "model": "app.Model",
    "pk": 1,
    "fields": {}
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM