Is django grok yml? django does not load YML file documents (yml is not a known serialization)

I successfully created my first django project.

I have two applications in my project foo and foobar.

I created a folder called "fixtures" in each application folder. I did NOT specify the snap-in directory in my .yml settings, so ( according to the docs ), django should look in my app for the {app} / document folder.

In the {app} / fixtures folder, I have several YML files. I split the source data for the various modules into separate YML files, making sure that there are no dependencies between the files (i.e. all related models are in the same YML file, and the ancestors are found in the file before the models they use).

However, when I started. /manage.py syncdb after successfully creating db objects, the following message appeared:

No fixtures found

Then I tried to manually load the devices using the loaddata command:

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

Is the documentation referenced in the link above incorrect ?, or do I need to install a module so that django infects YML?

BTW, YML files are processed correctly and were checked for correctness (I successfully used them in another project) - so this is not a problem

[change]

I installed PyYaml and renamed my files according to the instructions of Manoj. I can get a little further down the line, but I still run into problems (BTW, I am using PyYaml 3.0.9).

Here is the model in my ORM project (ie {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)

Here is the YAML file that I am importing:

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 }

Here is the stack trace at startup. /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
+5
2

. - loaddata , . 0100_foobar.yaml ( ).

, .

PS: YAML PyYAML. , PyYAML.

OP . YAML, OP as-is, .

admin 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, , ?

+7

, , ​​ , .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 .

+1

All Articles