2010-10-07 75 views
5

我已经成功创建了我的第一个django项目。django grok YML? django不加载装置YML文件(yml不是已知的序列化)

我在我的项目foo和foobar中有两个应用程序。

我在每个应用程序文件夹中都创建了一个名为'fixtures'的文件夹。我没有在settings.yml中指定fixtures目录,因此(according to the docs),django应该在我的{app}/fixtures文件夹中查找。

在{app}/fixtures文件夹中,我有几个YML文件。我将各个模块的初始数据拆分为单独的YML文件,确保没有跨文件依赖关系(即,所有相关模型都位于相同的YML文件中,祖先在使用它们的模型之前出现在文件中)。

然而,当我run./manage.py执行syncdb成功创建了分贝对象后,有以下信息:

未发现夹具

然后我试图手动加载夹具通过使用loaddata命令:

./manage.py loaddata 0100_foobar.yml 
Problem installing fixture '0100_foobar': yml is not a known serialization 

是在上述错误给出的链接文件?或者我需要为安装模块Django的神交YML?

顺便说一句,在YML文件解析正常,并已检查正确性(我用他们成功地在其他项目中) - 所以这不是问题

[编辑]

我已经安装了PyYaml并根据Manoj的说明将我的灯具文件重新命名。我能够得到更多的线,但我仍然遇到问题(顺便说一句,我使用PyYaml 3.0.9)。

这是在我的项目ORM(即{}应用/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 /灯具/货币.yaml

[email protected]:~/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 
+0

我已投票结束了它。希望其他人会同意并删除它。 – 2010-10-07 11:02:05

+0

感谢Manoj。我以为我有答案,但事实证明我错了,所以我再次重新提交了这个问题,稍作修改以反映我的新发现 – skyeagle 2010-10-07 11:03:52

+0

oops。不幸的是我不能撤回我的投票。 – 2010-10-07 11:04:43

回答

7

我试图重现您的问题在我的项目之一秒。显然loaddata预计文件的扩展名与序列化格式相匹配。在你的情况下,你应该把你的文件重命名为0100_foobar.yaml(注意新的扩展名)。

本地测试显示我的假设是正确的。

PS:YAML序列化需要PyYAML库。如果您尚未安装,请安装PyYAML

更新

我复制了OP的模型,我的项目之一。当我试图加载OP给出的示例YAML时,我得到了相同的错误。

之后,我使用管理应用添加了一些数据,并使用django.core.serializers.serialize以YAML格式转储数据。

from django.core.serializers import serialize 
from app.models import Currency 
print serializers.serialize("yaml", Currency.objects.all()) 

我得到的结果与OP发布的结果有很大不同。见下文。我为这个模型添加了三个实例,并且它们都显示出来了。

- fields: {long_name: Australia - Dollars, rounding: 2, short_name: AUD, spot_settle: 0} 
    model: app.currency 
    pk: 1 
- fields: {long_name: Canada - Dollars, rounding: 2, short_name: CAD, spot_settle: 0} 
    model: app.currency 
    pk: 2 
- fields: {long_name: Euro Member Countries - Euro, rounding: 2, short_name: EUR, 
    spot_settle: 0} 
    model: app.currency 
    pk: 3 

我能够无任何麻烦地加载这些数据。

鉴于上述情况,我怀疑OP的YAML文件有问题。 @skyeagle,你可以试试倾销现有的数据和然后尝试加载转储回来?

+0

感谢您的答案 - 这将花费我很长的时间来找出文件名需要扩展更改。遵循您的建议后,我仍然遇到问题。我修改了我的问题以反映新的事态。我的YML文件有问题吗?它在另一个项目中工作正常...... – skyeagle 2010-10-07 11:49:00

+0

感谢您的侦探工作。我想我可以从这一点开始解决我的问题。我必须冲上去赶飞机。我现在接受你的答案,并且今天晚些时候我会尝试着解决它。 – skyeagle 2010-10-07 12:25:50

+0

我错过了PyYAML;谢谢。请参阅https://docs.djangoproject.com/en/1.6/topics/serialization/#serialization-formats。 – Seth 2014-01-12 15:59:26

1

对于像我这样的人谁只是固执,真的,真的,想要使用文件与.yml扩展,你可以在启动时注册一个串行使loaddata认识夹具文件:

from django.apps import AppConfig 
from django.core.serializers import register_serializer 


class MyAppConfig(AppConfig): 
    name = 'my_app_name' 
    label = 'my_app_label' 
    verbose_name = 'this is my really cool app' 

    def ready(self): 
     register_serializer('yml', 'django.core.serializers.pyyaml') 

通话到register_serializer将注册yml作为公认的扩展。