0

Does Django support node &anchors and *references in YAML fixtures?

Example of how a YAML fixture with an anchor and a reference would look like:

- model: auth.group
  pk: &somegroup 1
  fields:
    name: "some group"

- model: auth.user
  pk: 1
  fields:
    groups: [*somegroup]
    username: someuser
Anton Strogonoff
  • 32,294
  • 8
  • 53
  • 61

2 Answers2

2

It all depends on how you define adequately, but yes this code will load and the value for groups in Python will be the list [1]. There are two things important things to note:

  • Django uses PyYAML, which only supports (most of) the YAML 1.1 standard from 2005, not the newest YAML 1.2 standard from 2009.
  • For references to primitive scalars (like the integer in your example) the reference in the data representation in Python will be lost. I.e. you will end up with two integer values, and changing the one will not change the other, nor will dumping the data structure recreate the anchor/reference.
Anthon
  • 69,918
  • 32
  • 186
  • 246
  • Thanks for details on PyYAML implementation. Their docs are pretty messy, hard to find any sort of changelog. For [my own answer](http://stackoverflow.com/a/38264520/247441) I had to hand-test it basically. – Anton Strogonoff Jul 12 '16 at 12:24
  • In my case losing references is fine, they wouldn’t be preserved in Django DB anyway. Their only point is to reduce repetition at YAML generation stage. – Anton Strogonoff Jul 12 '16 at 12:25
0

Django delegates YAML fixture parsing to third-party library PyYAML, which does support references.

Loading fixtures with references seems to be working fine for me, though my use case is very simple—just primary key values, helps avoid repetition when programmatically generating fixtures from Jinja2 templates based on Ansible inventory.

Anton Strogonoff
  • 32,294
  • 8
  • 53
  • 61