我已经成功创建了我的第一个 django 项目。
我的项目 foo 和 foobar 中有两个应用程序。
我在每个应用程序文件夹中创建了一个名为“fixtures”的文件夹。我没有在我的 settings.yml 中指定一个fixtures 目录,所以(根据文档),django 应该在我的 {app}/fixtures 文件夹中查找。
在 {app}/fixtures 文件夹中,我有几个 YML 文件。我已经将各个模块的初始数据拆分为单独的 YML 文件,确保没有跨文件依赖关系(即所有相关模型都在同一个 YML 文件中,并且祖先出现在使用它们的模型之前的文件中)。
但是,当我在成功创建 db 对象后运行 ./manage.py syncdb 时,出现以下消息:
没有找到固定装置
然后我尝试使用 loaddata 命令手动加载固定装置:
./manage.py loaddata 0100_foobar.yml
Problem installing fixture '0100_foobar': yml is not a known serialization
上面链接中给出的文档是错误的吗?还是我需要安装一个模块才能让 django 了解 YML?
顺便说一句,YML 文件解析正确并已检查正确性(我在另一个项目中成功使用它们) - 所以这不是问题
[编辑]
我已经按照 Manoj 的说明安装了 PyYaml 并重命名了我的设备文件。我可以更进一步,但我仍然遇到问题(顺便说一句,我使用的是 PyYaml 3.0.9)。
这是我的项目 ORM 中的模型(即 {app}/model.py):
class Currency(models.Model):
short_name = models.CharField(max_length=3, db_index=True, unique=True, null=False) # ISO Code
long_name = models.CharField(max_length=64, db_index=True, unique=True, null=False)
spot_settle = models.IntegerField(null=False, default=0)
rounding = models.IntegerField(null=False, default=2)
这是我要导入的 YAML 文件:
Currency:
currency_aud : { short_name: AUD , long_name: Australia - Dollars , spot_settle: 0, rounding: 2 }
currency_cad : { short_name: CAD , long_name: Canada - Dollars , spot_settle: 0, rounding: 2 }
currency_eur : { short_name: EUR , long_name: Euro Member Countries - Euro , spot_settle: 0, rounding: 2 }
currency_gbp : { short_name: GBP , long_name: United Kingdom - Pounds , spot_settle: 0, rounding: 2 }
currency_jpy : { short_name: JPY , long_name: Japan - Yen , spot_settle: 0, rounding: 2 }
currency_usd : { short_name: USD , long_name: United States Of America - Dollars , spot_settle: 0, rounding: 2 }
currency_zar : { short_name: ZAR , long_name: South Africa - Rand, spot_settle: 0, rounding: 2 }
currency_hkd : { short_name: HKD , long_name: Hong Kong Dollar, spot_settle: 0, rounding: 2 }
currency_nzd : { short_name: NZD , long_name: New Zealand Dollar, spot_settle: 0, rounding: 2 }
currency_sgd : { short_name: SGD , long_name: Singapore Dollar, spot_settle: 0, rounding: 2 }
currency_dkk : { short_name: DKK , long_name: Danish Krone, spot_settle: 0, rounding: 2 }
currency_sek : { short_name: SEK , long_name: Swedish Krona, spot_settle: 0, rounding: 2 }
currency_chf : { short_name: CHF , long_name: Swiss Franc, spot_settle: 0, rounding: 2 }
这是我运行 ./manage.py loaddata myapp/fixtures/currencies.yaml 时的堆栈跟踪
me@somebox:~/work/demo/myproj$ ./manage.py loaddata reference/fixtures/0100_currency.yaml
Installing yaml fixture 'reference/fixtures/0100_currency' from absolute path.
Problem installing fixture 'reference/fixtures/0100_currency.yaml': Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/loaddata.py", line 165, in handle
for obj in objects:
File "/usr/local/lib/python2.6/dist-packages/django/core/serializers/pyyaml.py", line 57, in Deserializer
for obj in PythonDeserializer(yaml.load(stream), **options):
File "/usr/local/lib/python2.6/dist-packages/django/core/serializers/python.py", line 84, in Deserializer
Model = _get_model(d["model"])
TypeError: string indices must be integers, not str