Loading data using YAML files

A third format for data import is YAML, which gives you less overhead than XML, but still more control than CSV. Traditionally, it was used for tests rather than for data import, which is why you have more possibilities to call code here.

How to do it...

Take the following steps to translate the first recipe in this chapter to YAML:

  1. Add in a file called data/demo.yml to your manifest, in the demo section:
        'demo': [
            'data/demo.yml',
        ],
  2. Add content to this file:
    -
        !record {id: author_af, model: res.partner}
            name: Alexandre Fayolle
    -
        !record {id: author_dr, model: res.partner}
            name: Daniel Reis
    -
        !record {id: author_hb, model: res.partner}
            name: Holger Brunn
    -
        !record {id: book_cookbook, model: library.book}
            name: Odoo cookbook
            short_name: cookbook
            date_release: 2016-03-01
            author_ids: [(6, 0, [ref('author_af'), ref('author_dr'), ref('author_hb')])]
            publisher_id: res_partner_packt
  3. Add a file called data/res_partner.yml to your manifest, in the data section:
        'data': [
            'data/res_partner.yml',
        ],
  4. Add content to this file:
    -
        !record {id: res_partner_packt, model: res.partner}
            name: Packt publishing
            city: Birmingham
            country_id: base.uk

We did the same as in the first recipe, but in YAML syntax. Note that YAML files need the extension .yml to be recognized correctly.

How it works...

Odoo introduces a YAML datatype record, which is where most of the Odoo-specific behavior is implemented. As with XML, a record needs an ID and a model set in order to do something useful.

For field values, you use YAML's standard notation, which means you don't need quoting for strings. When filling many2one fields, Odoo will simply presuppose that what you give here is a XML ID, so there is no need for extra marking here either.

There's more...

We've seen that linking one2many and many2many fields is a bit awkward with XML and CSV, and in the example above, we used the same syntax to do the linking. In YAML, you could have done it more elegantly by simply writing the following:

-
    !record {id: book_cookbook, model: library.book}
        name: Odoo cookbook
        short_name: cookbook
        date_release: 2016-03-01
        author_ids:
        -   name: Alexandre Fayolle
        -   name: Daniel Reis
        -   name: Holger Brunn

This way, you created three records of type res.partner, linked in the field author_ids. Note that here also, creating records inline means they have no XML ID, which makes it difficult to refer to them from other places.

See also

This recipe focused on using YAML files to load data. If you're interested in testing using YAML files, refer to Chapter 7, Debugging and Automated Testing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.117.157